将 UIViewController 添加到 UINavigationController 时出现问题

发布于 2024-11-14 22:21:34 字数 1769 浏览 3 评论 0原文

我的 RootViewController 是一个 UITableViewController。以编程方式添加 UINavigationController:

_navigationController = [[[UINavigationController alloc] initWithRootViewController:_rootViewController] autorelease];
[self.window addSubview:_navigationController.view];
[self.window makeKeyAndVisible];

在 RootViewController.m 中,选择一行时应加载 DetailViewController:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSLog(@"Switch to detail");
  CCouchDBDocument *selectedObject = [self.contentsList objectAtIndex:indexPath.row];
  DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
  [self.view addSubview:detailViewController.view];
  [detailViewController setDetailItem: selectedObject]; 
  [self.navigationController pushViewController:detailViewController animated:YES];
  [detailViewController release];
}

如果没有 addSubView,屏幕上不会发生任何事情。我之前见过的所有例子都只使用了pushViewController。并且,加载 DetailView 大约需要 4 秒。这太长了(目前是空的,只有一个标签)。当我尝试设置 navigationTitle (self.title = @"Hello";) 时,标题与 RootViewController 保持相同,因此 navigationController 一定有问题。

我尝试将所有内容放入 AppDelegate 中并使用 switchView 方法。问题是对 setDetailItem 的调用,如果我使用 switch 方法,则无法调用它。

将 DetailView 从 RootViewController 加载到导航堆栈中以及稍后可能从 DetailViewController 加载更多内容的正确方法是什么?

更新 我再次从头开始使用基于 Window 的应用程序。添加了一个 UITableViewController 作为“RootViewController”,并使用 AppDelegate 中的 UINavigationController 对其进行初始化(在 XIB 中完全没有执行任何操作)。当我尝试设置 self.navigationController.title = @"Test";在 ViewDidLoad 中,什么也没有发生。

那里出了什么问题?

My RootViewController is a UITableViewController. A UINavigationController is added programmatically:

_navigationController = [[[UINavigationController alloc] initWithRootViewController:_rootViewController] autorelease];
[self.window addSubview:_navigationController.view];
[self.window makeKeyAndVisible];

Within the RootViewController.m the DetailViewController should be loaded when a row is selected:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSLog(@"Switch to detail");
  CCouchDBDocument *selectedObject = [self.contentsList objectAtIndex:indexPath.row];
  DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
  [self.view addSubview:detailViewController.view];
  [detailViewController setDetailItem: selectedObject]; 
  [self.navigationController pushViewController:detailViewController animated:YES];
  [detailViewController release];
}

Without addSubView nothing happens on the screen. All the examples I've seen before only use pushViewController. And, loading the DetailView takes about 4 seconds. That's way too long (it's currently empty, just one label). When I try to set the navigationTitle (self.title = @"Hello";), the title remains the same from the RootViewController, so something must be wrong with the navigationController.

I tried to put everything in the AppDelegate and use a switchView method. The problem is the call for setDetailItem, which I can't call if I work with the switch method.

What would be the correct way to load the DetailView from the RootViewController into the navigation stack and possibly more from the DetailViewController later?

Update
I started from the beginning again with a Window-based application. Added a UITableViewController as "RootViewController" and initialised it with the UINavigationController in the AppDelegate (did absolutely nothing in the XIB). When I try to set self.navigationController.title = @"Test"; in ViewDidLoad, nothing happens.

What's wrong there?

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

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

发布评论

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

评论(1

千柳 2024-11-21 22:21:34

当使用 UINavigationController 通过 self.title 显示 DetailView 时,您不需要设置 DetailView 的标题,您需要在 DetailView 初始值设定项中设置 UINavigationItem title 属性。

例如在 DetailView 初始值设定项中:-

self.navigationItem.title = @"Hello";

你是对的,你不需要将DetailViewController 视图添加为当前视图的子视图 - 你应该只需要 PushViewController 调用。我不确定为什么它没有出现。

明显的问题是笔尖中的所有连接是否正常,以及 DetailView 初始值设定项的作用是什么?

You don't set the title of the DetailView when it's displayed using a UINavigationController by using self.title, you need to set the UINavigationItem title property in the DetailView initializer.

e.g. in the DetailView initializer :-

self.navigationItem.title = @"Hello";

You're right you shouldn't need to add the detailViewController view as a subview of the current view - you should just need the pushViewController call. I'm not sure why it's not appearing though.

Obvious questions are is everything connected OK in the nib, and what does the DetailView initializer do?

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