iPhone : 笔尖文件 +代码=混合
我对何时应该使用 NIB 文件以及何时应该使用代码感到有点困惑。 这是我的问题:
我有一个基于导航的应用程序,带有 RootController 及其 NIB 文件。 RootController 的 NIB 文件包含一个 TableView。 当我单击一个单元格时,我会初始化一个新连接,并请求加载内容。 当连接完成加载时,我从 NIB 文件创建一个新的 postViewController (自定义),然后将其推送到 navigationController viewController 堆栈上,如下所示:
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];
然后,如您所见,我尝试将 rootViewController 设置为 webView 的委托能够拦截链接上的点击并将新的 ViewController 推送到堆栈上。我需要新的视图来拥有带有后退按钮的导航栏。
问题:看起来 setDelegate 不起作用,因为 webView:shouldStartLoadWithRequest:navigationType 永远不会被调用!
我想我应该在 NIB 文件中设置委托,但我不知道如何设置。 PostViewController 中的 NIB 文件不知道 RootViewController...
以下是 NIB 文件的屏幕截图:
如果您需要更多详细信息,请询问我。
非常感谢...帮助我不再碰头:)
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 :
If you need more detail just ask me.
Thanks a lot...for helping me not banging my head for another day :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您调用
setDelegate:
时,请确保postViewController.webView
不为 nil。还要确保它没有在其他任何地方被调用,并确保委托插座没有连接到 NIB 中的任何东西。其他一些评论:
在这种情况下使用根视图控制器作为 webview 的委托有点不寻常。它应该可以工作,但是将这些委托方法移到 PostViewController 中可能更有意义。从那里您仍然可以拦截链接点击并调用
[self.navigationController PushViewController:animated:]
。[PostViewController release]
正在释放类对象。这不是一个好主意。相反,您应该调用[postViewController release]
。Make sure
postViewController.webView
isn't nil when you callsetDelegate:
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:
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:]
.[PostViewController release]
is releasing the class object. Not a good idea. Instead you should be calling[postViewController release]
.[postViewController.webView setDelegate:self];
设置当前控制器的委托,而不是postViewController
。尝试:您可以将此行放在
pushViewController:animated:
下面,那么 webView 应该已经存在。[postViewController.webView setDelegate:self];
sets the delegate for the current Controller notpostViewController
. Try:And you can put this line below
pushViewController:animated:
then the webView should already exist.