0

I'm getting a little confused about when I should use a NIB file and when I should use code. Here's my problem :

I have a navigation based application with a RootController and its NIB file. The RootController's NIB file contains a TableView. When I click on a cell I initialize a new connection with a request to load content. When the connection has finished loading I create a new postViewController (custom) from a NIB file and I push it on the navigationController viewController stack like that :

PostViewController *postViewController = [[PostViewController alloc] initWithNibName:@"PostViewController" bundle:[NSBundle mainBundle]]; 

[postViewController.webView setDelegate:self];

postViewController.postContent = [[postsData objectForKey:@"post"] objectForKey:@"content"];

[self.navigationController pushViewController:postViewController animated:YES];

[PostViewController release];

Then, as you can see I try to set the rootViewController as the delegate for the webView in order to be able to intercept a click on a link and push a new ViewController on the stack. I need that new view to have the navigation bar with the back button.

Problem : looks like the setDelegate isn't working because the webView:shouldStartLoadWithRequest:navigationType never gets called !

I guess I should set the delegate in the NIB file but I have no idea how. The NIB file from PostViewController doesn't know about the RootViewController...

Here are screenshots of the NIB files :

RootViewController.xib

PostViewController.xib (File's Owner)

PostViewController.xib (Web View)

If you need more detail just ask me.

Thanks a lot...for helping me not banging my head for another day :)

teum
  • 125
  • 3
  • 12
  • 1
    Have you checked that postViewController.webview exists when you try to set the delegate ? I'd bet it's nil, because the view is not loaded yet. If that's the case, you have to find another way to set your RootVC as the PostVC's Webview Delegate, like using a property inside your PostVC, and your PostVC should set the webview delegate in the ViewDidLoad method I suppose – scalbatty Apr 08 '11 at 15:27
  • Actually, if the nib's outlets are set correctly, webView shouldn't be nil (I think). Maybe there's something wrong in the nib file? – Cameron Spickert Apr 08 '11 at 15:42
  • @Luzal : you are right, postViewController.webView is nil when I try to set it as the delegate. I keep having that kind of problem, if you have some advice to give me on that subject I'll be happy to hear them... – teum Apr 11 '11 at 08:14
  • @Cameron : I guess you're right too, I'll try to edit my post with screenshots of my nib files because I want to figure this out. – teum Apr 11 '11 at 08:15
  • I'm sorry but I need one more reputation point to post images...grr. :| – teum Apr 11 '11 at 08:27
  • Just make sure the "webView" outlet of your postViewController is properly connected to the UIWebView in the nib file. You know how to do this, right ? – scalbatty Apr 11 '11 at 09:16
  • Yes I know how to do this for sure, and it is properly connected. – teum Apr 11 '11 at 11:15
  • The screenshots are in, I don't know how but now I have 12 reputation points :) – teum Apr 11 '11 at 11:23

2 Answers2

3

Make sure postViewController.webView isn't nil when you call setDelegate: on it. Also make sure it isn't being called anywhere else, and make sure the delegate outlet isn't connected to anything in your NIB.

A couple other comments:

  1. It's a bit unusual to use your root view controller as the webview's delegate in a case like this. It should work, but it might make more sense to move those delegate methods down into your PostViewController. From there you can still intercept the link clicks and call [self.navigationController pushViewController:animated:].

  2. [PostViewController release] is releasing the class object. Not a good idea. Instead you should be calling [postViewController release].

cduhn
  • 17,818
  • 4
  • 49
  • 65
  • Yes, it is nil. But it isn't being called anywhere else and the delegate outlet isn't connected to anything in my NIB. 1. That's what I was thinking, then I tried to move everything into the PostViewController but I failed in making it the Web View Delegate from there. I don't remember where I dit it and how (maybe something was wrong), I changed everything because it wasn't working. 2. True :o (or should I say YES) – teum Apr 11 '11 at 08:48
  • In fact it worked when I moved everything into the PostViewController, but I ran into another problem. I submitted another question for that [here](http://stackoverflow.com/questions/5619755/iphone-webviewshouldstartloadwithrequestnavigationtype-timing-problem). – teum Apr 11 '11 at 10:12
  • The other problem is solved too now, thank you for your help. – teum Apr 11 '11 at 12:05
  • Are you sure `postViewController.webView` is nil on the `setDelegate:` line? Based on the screen shots you posted here, it shouldn't be. Set a breakpoint on that line. When you hit it, type this in your gdb console: `po [postViewController webView]`. What does it say? – cduhn Apr 11 '11 at 12:09
1

[postViewController.webView setDelegate:self]; sets the delegate for the current Controller not postViewController. Try:

[postViewController.webView setDelegate:postViewController];

And you can put this line below pushViewController:animated: then the webView should already exist.

  • if your purpose is to have the RVC as delegate, than fine, but for good MVC code design it should be responsible for it's own content not anyone else. –  Apr 08 '11 at 15:48
  • I felt it like that too that's why I tried to make it that way, but then I ran into another problem (I remember now...). I submitted another question for that [here](http://stackoverflow.com/questions/5619755/iphone-webviewshouldstartloadwithrequestnavigationtype-timing-problem). – teum Apr 11 '11 at 10:10
  • The other problem is solved too now, thank you for your help. – teum Apr 11 '11 at 12:04