开发者

iPhone : nib file + code = mix up

开发者 https://www.devze.com 2023-02-22 21:08 出处:网络
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'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 :

iPhone : nib file + code = mix up

iPhone : nib file + code = mix up

iPhone : nib file + code = mix up

If you need more detail just ask me.

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


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].


[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.

0

精彩评论

暂无评论...
验证码 换一张
取 消