---
title: "UINavigationController实现页面跳转"
author: "xiaotongeye"
date: "2014-09-19T20:31:07Z"
tags: []
categories: ["编程语言"]
slug: "uinavigationcontroller"
draft: false
lastmod: "2014-09-22T17:57:43Z"
id: 2118317
url: "https://xiaotongeye.iteye.com/blog/2118317"
---

总结一下利用navigationController pushviewController 的方式实现页面跳转：
设置导航栏：

```AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end

```


```AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ViewController *viewController = [[viewcontroller alloc]init];
    UINavigationController *navigationController = [[uinavigationcontroller alloc]initWithRootViewController:viewController];
    self.window.rootViewController = navigationController;    
    return YES;
}

```


```ViewController.m

#import "SecondLevelViewController.h"

#pragma mark -
#pragma mark Table View Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  
    SecondLevelViewController *nextController = [[secondlevelviewcontroller alloc]init];
//获取正确的控制器
    [self.navigationcontroller pushviewcontroller:nextcontroller animated:yes];
//使用navigationController属性将下一个控制器放入到导航控制器栈中
}

```

以上是点击tableviewcell实现页面的跳转，若想利用button实现页面的跳转只需将
``` 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

```

改为
``` 

- (void)onClick:(id)sender

```

其中onClick是对应于button的事件
``` 
[button addtarget:self action:@selector(onclick:) forcontrolevents:uicontroleventtouchupinside];

```
中的onClick，若想要实现不同的button跳转到不同的页面可以改变onClick实现。
补充一点：若想直接返回上一界面，则只需在事件发生的下面加入代码
``` 

[self.navigationcontroller popviewcontrolleranimated:yes];
```