IOS学习(六)导航视图控制器

Appdelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];
    UINavigationController *navigationCtonroller =[[UINavigationController alloc] initWithRootViewController:[[FirstViewController alloc] init]];
    [self.window setRootViewController:navigationCtonroller];
    
    return YES;
}

FistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor grayColor];
    
    self.title = @"First";
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
    //设置系统图片,设置单个view
//    self.navigationItem.leftBarButtonItem = leftButton;
    
    //自定义itembutton
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 90, 30)];
    label.text = @"应用商店";
    label.backgroundColor = [UIColor grayColor];
    UIBarButtonItem *customButton = [[UIBarButtonItem alloc] initWithCustomView:label];
    [label targetForAction:@selector(pushController) withSender:self];
    //设置多个itmesButton
    self.navigationItem.leftBarButtonItems = @[leftButton,customButton];
    
    
    
    //设置导航控制器的颜色
//    self.navigationController.navigationBar.backgroundColor = [UIColor redColor];
    //设置导航控制器的背景图片
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"buttonPost"] forBarMetrics:UIBarMetricsDefault];
    
    
    UIButton *pushButton = [UIButton buttonWithType:UIButtonTypeCustom];
    pushButton.frame = CGRectMake(0, 100, 100, 30);
    pushButton.backgroundColor = [UIColor greenColor];
    [pushButton setTitle:@"push" forState:UIControlStateNormal];
    [pushButton addTarget:self action:@selector(pushController) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:pushButton];
}

- (void)pushController {
    NSLog(@"hehhe");
    
    //这里如果SecondViewController没有设置背景色,没有做其他操作,在跳转时,跳转动画将发生卡顿现象
    [self.navigationController pushViewController:[[SecondViewController alloc] init] animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"Second";
    self.view.backgroundColor = [UIColor blueColor];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"POP" style:UIBarButtonItemStylePlain target:self action:@selector(pop)];
    self.navigationItem.leftBarButtonItem = item;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"PUSH" style:UIBarButtonItemStylePlain target:self action:@selector(push)];
    self.navigationItem.rightBarButtonItem = rightItem;

    
}
- (void)push{
    [self.navigationController pushViewController:[[ThridViewController alloc] init] animated:YES];
}
- (void)pop {
    [self.navigationController popViewControllerAnimated:YES];
}

ThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"Third";
    self.view.backgroundColor = [UIColor greenColor];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"跳转" style:UIBarButtonItemStylePlain target:self action:@selector(push)];
    self.navigationItem.leftBarButtonItem = rightItem;

    
}
- (void)push {
    //跳转到根视图
//    [self.navigationController popToRootViewControllerAnimated:true];
    //销毁当前视图
//    [self.navigationController popViewControllerAnimated:YES];
    
    //获取导航控制器下所有的视图控制器
    NSLog(@"%@", self.navigationController.viewControllers);
    //跳转到指定视图控制器
    [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值