Snail—UI学习之导航视图控制器UINavigationController(系统)

背景

有一个根视图控制器 然后跳转到第一个界面  第一个界面能够返回到根视图 也能够跳转到第二个视图 第二个视图能够直接返回到根视图

新建三个ViewController    RootViewController FirstViewController SecondViewController

首先在AppDelegate.m中写入

#import "WJJAppDelegate.h"
#import "WJJRootViewController.h"

@implementation WJJAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    WJJRootViewController * rootViewController = [[WJJRootViewController alloc] init];
    //创建一个导航控制器 并把上面创建的root 加入到导航控制器上
    UINavigationController * nav = [[UINavigationController alloc]
                                    initWithRootViewController:rootViewController];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    return YES;
}

然后再RootViewController中写入 点击button会进入到firstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self createButton];
}

//新建一个button 点击进入写一个界面
- (void)createButton{
    UIButton * nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
    nextButton.frame = CGRectMake(40, 80, 240, 40);
    [nextButton setTitle:@"下一页" forState:UIControlStateNormal];
    [nextButton addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextButton];
}

- (void)nextPage{
    WJJFirstViewController * first = [[WJJFirstViewController alloc] init];
    //利用push方法 进入下一个界面 当返回上一个界面的时候 全部界面并不没有了 而是压到栈中
    //跟之前present那个跳转界面是不同的
    [self.navigationController pushViewController:first animated:YES];
}

在firstViewController中 点击左边按钮能够返回首页 点击右边按钮 进入下一页

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    //在导航控制器的左边、右边自己定义返回键、下一页键
    [self createBarButtonItem];
}

- (void)createBarButtonItem{
    
    UIButton * popButton = [UIButton buttonWithType:UIButtonTypeSystem];
    //假设放在导航栏的左右、自己定义的button跟x、y无关 仅仅跟宽高有关系
    popButton.frame = CGRectMake(0, 0, 50, 20);
    //设置标题
    [popButton setTitle:@"返回" forState:UIControlStateNormal];
    [popButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    //以popbutton创建一个自己定义的导航条button
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:popButton];
    //让导航栏的返回button 换成我们自己定义的
    self.navigationItem.leftBarButtonItem = item;
    
    
    UIButton * pushButton = [UIButton buttonWithType:UIButtonTypeSystem];
    //假设放在导航栏的左右、自己定义的button跟x、y无关 仅仅跟宽高有关系
    pushButton.frame = CGRectMake(0, 0, 50, 20);
    //设置标题
    [pushButton setTitle:@"下一页" forState:UIControlStateNormal];
    [pushButton addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
    //以pushbutton创建一个自己定义的导航条button
    UIBarButtonItem * item2 = [[UIBarButtonItem alloc] initWithCustomView:pushButton];
    //让导航栏的右边button 换成我们自己定义的
    self.navigationItem.rightBarButtonItem = item2;
    
    
}

- (void)nextPage{
    WJJSecondViewController * second = [[WJJSecondViewController alloc] init];
    [self.navigationController pushViewController:second animated:YES];
}

- (void)back{
    //pop即是把此视图压到栈里面 让上一个界面显示
    [self.navigationController popToRootViewControllerAnimated:YES];
}

然后,在secondViewController中点击button 返回首页

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    [self createToRootButton];
}

//新建一个button 点击返回首页
- (void)createToRootButton{
    UIButton * toRootButton = [UIButton buttonWithType:UIButtonTypeSystem];
    toRootButton.frame = CGRectMake(40, 80, 240, 40);
    [toRootButton setTitle:@"首页" forState:UIControlStateNormal];
    [toRootButton addTarget:self action:@selector(toRootPage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:toRootButton];
}

//点击按钮 返回到首页 有两种方式
- (void)toRootPage{

    //第一种 直接返回到首页
    //[self.navigationController popToRootViewControllerAnimated:YES];
    
    //另外一种 由于这些视图控制器是压栈、出栈操作,所以在视图控制器里有一个视图控制器的数组 首页下标为0
    [self.navigationController popToViewController:self.navigationController.viewControllers[0] animated:YES];
    
}

首页


第一个页面

第二个页面


posted @ 2017-06-16 15:54  yfceshi  阅读(170)  评论(0编辑  收藏  举报