【问题标题】:Problems adding UIViewController to UINavigationController将 UIViewController 添加到 UINavigationController 的问题
【发布时间】:2011-06-10 17:20:37
【问题描述】:

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

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

在rootviewController中,在选择行时应加载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 中,没有任何反应。

怎么了?

【问题讨论】:

    标签: objective-c ios4 uinavigationcontroller xcode4


    【解决方案1】:

    在使用 UINavigationController 通过 self.title 显示 DetailView 时,您无需设置其标题,您需要在 DetailView 初始化程序中设置 UINavigationItem 标题属性。

    例如在 DetailView 初始化程序中:-

    self.navigationItem.title = @"Hello";
    

    你是对的,你不需要将 detailViewController 视图添加为当前视图的子视图——你只需要调用 pushViewController 即可。我不确定为什么它没有出现。

    明显的问题是 nib 中的所有连接是否都正常,DetailView 初始化程序有什么作用?

    【讨论】:

    • 不,也没有改变。那里出了点严重问题。
    • 我认为你需要更改的是 self.navigationItem.title,not self.navigationController.title - 如果你以编程方式构建视图,你可能需要设置它在 loadView 中(我正在使用 nib 并将其设置在 initWithNibName 中:)
    • 还有其他一些问题。从头开始设置,现在一切正常。谢谢!
    猜你喜欢
    • 2011-06-24
    • 1970-01-01
    • 2012-02-07
    • 2013-07-20
    • 2011-05-13
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多