Add SubView (WebView) within NSView subclass - objective-c

OK, the scenario is pretty straightforward, but - since NSViews have never been my strongest point - I decided to ask for some help.
So...
We have an NSView subclass
When the NSView is created, we want to add a subview (WebView) which is going to occupy all the initial view's space + automatically resize in width/height.
And this is my code (which still does not work, as described) :
- (id) initWithFrame:(NSRect)frame {
NSLog(#"In INIT");
self = [super initWithFrame:frame];
if (self == nil) return nil;
[self setWebView:[[WebView alloc] initWithFrame:frame]];
[[self webView] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
NSString *path = [[NSBundle mainBundle] pathForResource:#"index"
ofType:#"html"];
NSString *htmlContent = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:nil];
[[[self webView] mainFrame] loadHTMLString:htmlContent
baseURL:[NSURL URLWithString:path]];
[self addSubview:[self webView]];
return self;
}
Any ideas?

Related

custom UIVIew with nib subviews nil

I create a simple custom UIView with a xib file, I connect xib view to my class, then I linked my IBOutles to subviews, after this I create the view with initWithFrame and I add it programmatically to my parent view
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:#"AddAlarmView" owner:self options:nil];
_scrollView = (UIScrollView *)[objects objectAtIndex:0];
[self addSubview:_scrollView];
}
return self;
}
the problem is that all the subviews are nil....if I try to get customView.textFeild1 it is nil....why?where is the mistake?
Instead of this you should try to get your UIView object from Xib like this..
- (id)initWithFrame:(CGRect)frame
{
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:#"AddAlarmView" owner:self options:nil];
self = (YourClassName *)[objects objectAtIndex:0];
self.frame = frame;
return self;
}
You don't need to add subViews again your IBOutLets. They are already subView of your View in xib file.
Simply load that xib file and assign it to your self and update your frame according to the parameter.

add subview is not displaying my uiview

I have the following code, and the uiview is not showing, i've placed a breakpoint & see the view is not nil and the frame is set to the given size - why the view is not showing in my uiviewcontroller?
#property (nonatomic,strong) LoadingView *loadingView;
self.loadingView = [[[NSBundle mainBundle] loadNibNamed:#"LoadingView" owner:self options:nil] objectAtIndex:0];
self.loadingView.frame = CGRectMake(110,170,100,100);
[self.view addSubview:self.loadingView];
Your code is logically wrong. Your LoadingView class must allocate before you assigning nib member to it.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:#"LoadingView" owner:self options:nil];
self = [nibs objectAtIndex:0];
}
return self;
}
Your nib assignment should be in the initwithframe method of LoadingView class.
Then Your implementation of adding view would be like,
self.loadingView = [[LoadingView alloc] initWithFrame:CGRectMake(110,170,100,100)];
[self.view addSubview:self.loadingView];

Increase Pages After Each Swipe Objective-C

I have one UIViewController and inside it I have 3 UIWebView. I added swipe left to my webViews programatically. Right now I can swipe, but just 3 pages and then repeat again. I want to increase my currentPage and add this to my webView3, but I don't know how should I do that.
Would you please give me some hints? I really have problem in this part!
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"test view");
NSURL *url = [NSURL fileURLWithPath:[ [ NSBundle mainBundle ] pathForResource: #"Name/Name1" ofType:#"html" ]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView2 loadRequest:request];
[webView2 bringToFront];
NSURL *url = [NSURL fileURLWithPath:[ [ NSBundle mainBundle ] pathForResource:
#"Name/Name2" ofType:#"html" ]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView3 loadRequest:request];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(swipeLeftAction:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.delegate = self;
[self.view addGestureRecognizer:swipeLeft];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer
*)otherGestureRecognizer
{
return YES;
}
currentPage is an integer and at the beginning it is 0. My question is how should I add my currentPage to webView3 so that when I Swipe it increase my pages?
- (void)swipeLeftAction:(id)ignored
{
NSLog(#"Swipe Left");
UIWebView *temp = webView2 ;
webView2 = webView3;
webView3 = webView;
webView = temp;
[webView2 bringToFront];
currentPage++;
}
Thanks in advance!
****Edit:**
This current page is not connected to any webView, How Can I get this increase in my webView?**
Based on information from this post (Does UIGestureRecognizer work on a UIWebView?) it looks like gesture recognizers and webviews do not always play nice together, the reason being because of a webview's own gesture recognizers. Perhaps the best way would be to add links inside of your webview that when touched use your own scheme to increment currentPage
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( [[[inRequest URL] absoluteString] hasPrefix:#"mySchemeIncrementPage:"] ) {
currentPage++;
NSLog(#"CURRENT PAGE COUNT: %i", currentPage);
return NO;
}
}

iOS: NSURLRequest returns the same page each time on different storyboards

I have a UIWebView class that is added to each storyboard on load, but when passing into this class the page I require form the parent ViewController it only show the file from the first ever page and on each different page/viewController its the same even though I set it different on each.
Is it somehow caching this page and if so, how can I have a clean UIWebview everytime?
my UIwebview class m file (ContentView.m):
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSString *urlpath = [[NSBundle mainBundle] pathForResource:pageToDisplay ofType:#"html" inDirectory:#"pageContent"];
NSURL *url=[[NSURL alloc] initFileURLWithPath:urlpath];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[self loadRequest:request];
}
return self;
}
My Parent view passes the requested page using the following variable pageToDisplay:
Parent View m file:
#import "ContentView.h"
- (void)viewDidLoad {
ContentView *dispalyPageContent = [[ContentView alloc] init];
[dispalyPageContent setPageToDisplay:#"THEpg1"];
[self.view addSubview:dispalyPageContent];
//--
[super viewDidLoad];
}
For each other parent view the code is the same but the [displayPageContent setPageToDisplay:#"THEpg1"]; which should change the page I want, e.g. next view would be [displayPageContent setPageToDisplay:#"THEpg2"];
Is it possible to clear this and load a fresh request each time?
It's because when you call ContentView *dispalyPageContent = [[ContentView alloc] init]; in your viewDidLoad , you init it with:
NSString *urlpath = [[NSBundle mainBundle] pathForResource:pageToDisplay ofType:#"html" inDirectory:#"pageContent"];
And in pathForResource:pageToDisplay at the moment of initializing is some trash.
And then, when you call [dispalyPageContent setPageToDisplay:#"smth"] , it changes nothing, because your class ContentView has been already inited with some trash in PageToDisplay
(Also, you call [[ContentView alloc] init]; and in your code there is only initWithFrame:... for objects of ContentView class)
You can solve this in this way:
1) in your ContentView.m
- (id)initWithFrame:(CGRect)frame pageToDisp:(NSString *)pageToDisp {
self = [super initWithFrame:frame];
if (self) {
NSString *urlpath = [[NSBundle mainBundle] pathForResource:pageToDisp ofType:#"html" inDirectory:#"pageContent"];
NSURL *url=[[NSURL alloc] initFileURLWithPath:urlpath];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[self loadRequest:request];
}
return self;
}
2) In your ContentView.h:
- (id)initWithFrame:(CGRect)frame pageToDisp:(NSString *)pageToDisp
3) And then, in viewDidLoad:
- (void)viewDidLoad {
ContentView *dispalyPageContent = [[ContentView alloc] initWithFrame:someFrame pageToDisp:#"THEpg1"];
[self.view addSubview:dispalyPageContent];
//--
[super viewDidLoad];
}

AVAudioPlayer causing memory leak

Not sure if this is a simulator issue or I am missing something, the following code gives me a memory leak although theAudio is released as in dealloc. I am playing a .wav file and the sound will change depending on the value of an integer. The App runs ok but 16 bit leak is flagged whwn I test this in xcode
Any suggestion is greatly appreciated:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface Water : UIViewController
<AVAudioPlayerDelegate>
{
}
#property (nonatomic,retain) AVAudioPlayer *theAudio;
-(IBAction) goButMenu: (id) sender;
-(IBAction) goDrinkMenu: (id) sender;
-(IBAction) playwater;
#end
#implementation Water
#synthesize theAudio;
-(IBAction) goDrinkMenu: (id) sender{
ButDrinkMenu *butdrinkmenu1 = [[ButDrinkMenu alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:butdrinkmenu1 animated:YES];
}
-(void) viewDidLoad {
if (BoyOrGirl == 1) {
NSString *path = [[NSBundle mainBundle] pathForResource:#"Applause" ofType:#"wav"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
else {
NSString *path = [[NSBundle mainBundle] pathForResource:#"bongo" ofType:#"wav"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
}
-(IBAction) playwater{
if (BoyOrGirl == 1) {
NSString *path = [[NSBundle mainBundle] pathForResource:#"Applause" ofType:#"wav"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
else {
NSString *path = [[NSBundle mainBundle] pathForResource:#"bongo" ofType:#"wav"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
}
Definitely test on the device directly; testing for leaks with the simulator may return inaccurate results.
That said, there are several memory-related concerns with this code sample. You may wish to include your dealloc method here as that may offer some insight, but here's a problem (that appears in several of your allocations):
Your AVAudioPlayer isn't being assigned to your property, nor is it being released in scope. Ideally a player allocation would look more like:
AVAudioPlayer *myPlayer = [[AVAudioPlayer alloc] initWithContents....
[myPlayer setDelegate:self];
[self setTheAudio:myPlayer];
[myPlayer release];
As opposed to how you've implemented it, which assigns the player directly the instance variable:
theAudio = [[AVAudioPlayer alloc] initWith...
Because you assign the player directly to your instance variable, your player isn't being retained. This is likely to result in either premature release and a dangling pointer if you try and balance your calls properly, or it will result in leaking.