我不知道您为什么希望在单视图应用程序中使用导航控制器,但您可以这样做。
如果您使用 Storyboard,请选择初始 ViewController 上的视图控制器图标,然后:
转到编辑器 -> 嵌入 -> 导航控制器
这将添加您的导航控制器。
如果您想以编程方式执行此操作,那么您将希望在AppDelegate 的方法didFinishLaunchingWithOptions 中实现此代码:
UIViewController *bbp=[[UIViewController alloc]initWithNibName:@"UIViewController" bundle:nil];
UINavigationController *passcodeNavigationController = [[UINavigationController alloc] initWithRootViewController:bbp];
// [self.navigationController presentModalViewController:passcodeNavigationController animated:YES];
[self.navigationController pushViewController:passcodeNavigationController animated:YES];
[passcodeNavigationController release];
如果你曾经对 Swift 感兴趣,我找到了this thread,并且接受的答案似乎相当不错:
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var nav1 = UINavigationController()
var mainView = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
nav1.viewControllers = [mainView]
self.window!.rootViewController = nav1
self.window?.makeKeyAndVisible()
这是来自 Objective-C 的转换代码(可能有错误):
var bbp: UIViewController = UIViewController(nibName: "UIViewController", bundle: nil)
var passcodeNavigationController: UINavigationController = UINavigationController(rootViewController: bbp)
// [self.navigationController presentModalViewController:passcodeNavigationController animated:YES];
navigationController.pushViewController(passcodeNavigationController, animated: true)
passcodeNavigationController