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

来源:互联网 发布:数控切割机编程代码 编辑:程序博客网 时间:2024/05/01 00:27

背景

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

新建三个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中写入 点击按钮会进入到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];    //如果放在导航栏的左右、自定义的按钮跟x、y无关 只跟宽高有关系    popButton.frame = CGRectMake(0, 0, 50, 20);    //设置标题    [popButton setTitle:@"返回" forState:UIControlStateNormal];    [popButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];    //以pop按钮创建一个自定义的导航条按钮    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:popButton];    //让导航栏的返回按钮 换成我们自定义的    self.navigationItem.leftBarButtonItem = item;            UIButton * pushButton = [UIButton buttonWithType:UIButtonTypeSystem];    //如果放在导航栏的左右、自定义的按钮跟x、y无关 只跟宽高有关系    pushButton.frame = CGRectMake(0, 0, 50, 20);    //设置标题    [pushButton setTitle:@"下一页" forState:UIControlStateNormal];    [pushButton addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];    //以push按钮创建一个自定义的导航条按钮    UIBarButtonItem * item2 = [[UIBarButtonItem alloc] initWithCustomView:pushButton];    //让导航栏的右边按钮 换成我们自定义的    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中点击按钮 返回首页

- (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];    }

首页


第一个页面

第二个页面


0 0