如何在代码中向视图添加UINavigationController?

Tha*_*ham 4 iphone uiviewcontroller uiview uinavigationcontroller

view1 = [[View1 alloc] init];   //Create the first view
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:view1];
navigationController1.navigationBar.tintColor =[UIColor blackColor];
Run Code Online (Sandbox Code Playgroud)

View1继承自UIViewController.所以我创建了一个*view1,然后我创建了一个UINavigationController,调用*navigationController1.我如何将两者联系在一起?非常感谢你

jke*_*esh 10

将视图控制器与导航控制器链接的方法是将视图控制器推送到导航堆栈.例如:

UIViewController * yourViewController = [[UIViewController alloc] init];
UINavigationController * navigation = [[UINavigationController alloc] init];
[navigation pushViewController:yourViewController animated:NO];
[yourViewController release]
Run Code Online (Sandbox Code Playgroud)

最后释放视图控制器,因为导航控制器会保留它.

  • 你可以用root视图初始化:UINavigationController*navigation = [[UINavigationController alloc] initWithRootViewController:yourViewController]; (2认同)