将UINavigationController添加到现有的UIViewController中。

9

如何将已存在的UIViewController(使用presentModalViewController呈现)添加到UINavigationController中?

当用户点击按钮时,需要推送我的详细视图的新副本。(换句话说,在UINavigationController中以模态方式显示pushViewController,然后再次pushViewController)。

实现此功能的最简单方法是什么?


不清楚你的问题是什么。你想以模态方式呈现UINavigationController吗?那样做感觉不太对,但我不能确定。 - Bogatyr
这是关于编程的内容,翻译成中文如下:这是简化后的描述...以模态方式呈现UINavigationController...(但修改现有的UIViewController)。 - marko
2个回答

37

如何创建模态视图控制器?只需将控制器包装到UINavigationController中。

假设您的模态代码如下:

MyExampleViewController *vc = [[[MyExampleViewController alloc] initWithNibName:@"MyExample" bundle:nil] autorelease];
[self presentModalViewController:vc animated:YES];

然后将其更改为类似于以下内容:

MyExampleViewController *vc = [[[MyExampleViewController alloc] initWithNibName:@"MyExample" bundle:nil] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:vc] autorelease];
[self presentModalViewController:navController animated:YES];

在navController的viewControllers属性中有MyExampleViewController和MyDetailViewController。但是屏幕没有更新(MyDetailViewController没有显示)? - marko
如何在MyExampleViewController中调用pushViewController? - marko
[self.navigationController pushViewController:anotherVC animated:YES]; ?! - Matthias Bauch
我使用这种方法。但是我无论如何都无法将UIBarButtonItem添加到带有navigationController的工具栏中。任何帮助将不胜感激! - cantfindaname88
self.navigationItem.rightBarButtonItem = yourUIBarButtonItem; 不起作用吗?顺便说一下,它不是工具栏,而是导航栏。也许你应该为你的问题创建一个新的提问。 - Matthias Bauch

0
我认为你需要在代理中添加一个导航控制器,这样你就可以从应用程序的任何地方推送视图了。
在AppDelegate.h文件中实现。
UINavigationController *navig;

在AppDelegate.M文件中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    navig = [[UINavigationController alloc] initwithRootViewController:viewController.view];

    //[navig pushViewController:viewController animated:YES];
    [self.window addSubview:navig.view];
    [self.window makeKeyAndVisible];

    return YES;
}

我既不能理解问题,也看不出这个答案的相关性 :)。 - Bogatyr
你想要推出新的详情视图控制器,所以你应该有导航控制器,然后才能推出。 - Splendid
如果您将向应用程序委托添加导航控制器,则只能从类中的任何位置推送视图。因为如果您的应用程序是基于视图的应用程序,则导航控制器不起作用,所以最好您应该在委托中添加导航控制器,这样您就可以轻松地推送视图。请查看我的答案,我已经进行了编辑。 - Splendid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接