iPhone : 笔尖文件 +代码=混合

发布于 2024-10-31 18:23:49 字数 1331 浏览 4 评论 0原文

我对何时应该使用 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 文件的屏幕截图:

RootViewController.xib

PostViewController.xib(文件所有者)

PostViewController.xib (Web View)

如果您需要更多详细信息,请询问我。

非常感谢...帮助我不再碰头:)

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欲拥i 2024-11-07 18:23:49

当您调用 setDelegate: 时,请确保 postViewController.webView 不为 nil。还要确保它没有在其他任何地方被调用,并确保委托插座没有连接到 NIB 中的任何东西。

其他一些评论:

  1. 在这种情况下使用根视图控制器作为 webview 的委托有点不寻常。它应该可以工作,但是将这些委托方法移到 PostViewController 中可能更有意义。从那里您仍然可以拦截链接点击并调用 [self.navigationController PushViewController:animated:]

  2. [PostViewController release] 正在释放类对象。这不是一个好主意。相反,您应该调用 [postViewController release]

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

少年亿悲伤 2024-11-07 18:23:49

[postViewController.webView setDelegate:self]; 设置当前控制器的委托,而不是 postViewController。尝试:

[postViewController.webView setDelegate:postViewController];

您可以将此行放在 pushViewController:animated: 下面,那么 webView 应该已经存在。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文