iOS开发好文

导航控制器下自定义按钮完成界面跳转

2015-12-03  本文已影响882人  ThEAll

import "AppDelegate.h"

import "RootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


import "RootViewController.h"

import "SecondViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

// 视图将要出现的时候
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSLog(@"Root___%@",self.navigationController.viewControllers);
}

// 导航栏右侧按钮回调方法
-(void)rightBarBtnAction:(UIBarButtonItem*)sender{
// push到下个界面
SecondViewController *secondVC = [[SecondViewController alloc]init];
[self.navigationController pushViewController:secondVC animated:YES];
}

// 按钮的回调方法,进入下一个界面
-(void)pushAction:(UIButton*)sender{
// 打印viewController,看一下当前被navC所管理的视图控制器们
NSArray *viewCs = self.navigationController.viewControllers;
NSLog(@"%@",viewCs);
// 进入到下一个界面(为导航控制器新添加一个要被管理的视图控制器)
SecondViewController *secondVC = [[SecondViewController alloc]init];

//  推送到下一个界面
/**
 *   pushViewController:将要显示的视图控制器
     animated:切换中是否需要动画
     push的操作,就是将所要被管理的视图控制器入栈,实际执行的动作就是:[self.navigationController.viewControllers addObjects:secondVC]在此处secondVC就会位于栈顶
     viewControllers:该属性的返回类型为NSArray,但是我们一定得记得,viewControllers实际的类型是NSMutableArray。这里体现了面向对象多态的特性
 */
[self.navigationController pushViewController:secondVC animated:YES];
NSLog(@"push*****%@",self.navigationController.viewControllers);

//  得到当前位于栈顶的视图控制器  viewControllers这个数组的最后一个元素,一般都在栈顶
NSLog(@"top____%@",self.navigationController.topViewController);

//  得到当前正在显示的视图控制器
NSLog(@"visible~~~~~~%@",self.navigationController.visibleViewController);

}


import "SecondViewController.h"

import "ThirdViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

UIButton *thirdBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[thirdBtn setFrame:CGRectMake(100, 200, 100, 100)];
[thirdBtn setTitle:@"thirdBtn"
         forState:UIControlStateNormal];
[thirdBtn addTarget:self
            action:@selector(thirdAction:)
  forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:thirdBtn];

}

// 左边按钮的回调方法
-(void)leftBackBtnAction:(UIBarButtonItem*)sender{
[self.navigationController popToRootViewControllerAnimated:YES];
}

// 返回上级界面(出栈)
-(void)popAction:(UIButton*)sender{
// 出栈 实际所进行的操作是:[self.navigationController.viewControllers removeObject:self]
// 更准确的写法:[self.navigationController.viewControllers removeObject:self.navigationController.viewControllers.lastObject]
// 在出栈之前打印当前navC过管理的视图控制器
NSLog(@"pre ***%@",self.navigationController.viewControllers);
// [self.navigationController popViewControllerAnimated:YES];
// 返回到指定视图控制器
[self.navigationController popToViewController:self.navigationController.viewControllers.firstObject animated:YES];
// 出栈之后,再次打印当前navC所管理的视图控制器
NSLog(@"pop ~~~%@",self.navigationController.viewControllers);

}

// 跳转到thirdVC
-(void)thirdAction:(UIButton*)sender{

ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
[self.navigationController pushViewController:thirdVC animated:YES];

}

/*

pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

@end

import "ThirdViewController.h"

@interface ThirdViewController ()

@end

@implementation ThirdViewController

}

-(void)thirdBackRootAction:(UIButton*)sender{
// 返回根视图控制器
NSLog(@"third___%@",self.navigationController.viewControllers);
[self.navigationController popToRootViewControllerAnimated:YES];
NSLog(@"thirdPop___%@",self.navigationController.viewControllers);

}

/*

pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

@end

上一篇 下一篇

猜你喜欢

热点阅读