导航控制器

原创 2015年07月08日 10:37:41

导航控制器从某个视图控制器跳转到根视图控制器

-(NSArray *)popToRootViewControllerAnimated:(BOOL)animated;


导航控制器如何执行跳转到指定的视图控制器

-(NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;


添加RootViewController 类

AppDelegate.m

加上头文件#import "RootViewController.h"

-(BOLL).......

{

 //创建一个导航控制器的实例

    RootViewController * root = [[RootViewControlleralloc]init];

//会使root的引用加1

    UINavigationController * navController = [[UINavigationControlleralloc]initWithRootViewController:root];

//会使root的引用减为1

    self.window.rootViewController = navController;


self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

self.window.backgroundColor = [UIColorwhiteColor];

    [self.windowmakeKeyAndVisible];

    return YES;

}

-(void)viewDidLoad

{

    self.view.backgroundColor = [UIColorredColor];

}



添加ViewController2类

RootViewController.m

加上头文件#import "ViewController2.h"

-(void)viewDidLoad

{

   UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [btn setTitle:@"push to vc2"forState:UIControlStateNormal];

    [btn setFrame:CGRectMake(20,100,300, 50)];

    [btn addTarget:selfaction:@selector(btnClicked:)forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

//navigationItem的设置

    [self.navigationItem setTitle:@"root"];

    //设置的framex,y值不会起作用,视图会自动居中显示在导航条上

    UIView * titleView = [[UIView alloc] initWithFrame:CGRectMake(100, 50, 100, 40)];

    [titleView setBackgroundColor:[UIColor yellowColor]];

    //设置标题视图

    [self.navigationItem setTitleView:titleView];

    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;

    

    //通过初始化标题的方法得到一个UIBarButtonItem实例,plain风格时,按钮颜色会随着导航条的风格自动变化,done,按钮一致处于一种颜色

    UIBarButtonItem * item1 = [[UIBarButtonItem alloc] initWithTitle:@"左侧" style:UIBarButtonItemStyleBordered target:self action:@selector(itemClicked:)];

    //通过初始化图片的方式创建item实例,如果传递的图片尺寸过大,会被自动压缩,最适宜尺寸是20*20像素

    UIBarButtonItem * item4 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"1.png"] style:UIBarButtonItemStylePlain target:self action:@selector(itemClicked:)];

    

    NSArray * arrayItem2 = [NSArray arrayWithObjects:item1,item4, nil];

    self.navigationItem.leftBarButtonItems = arrayItem2;

//接收一个数组,数组中的元素是UIBarButtonItem的实例

    //初始化利用系统风格的样式

    UIBarButtonItem * item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(itemClicked:)];

    

    UIButton * btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [btn1 setFrame:CGRectMake(0, 0, 30, 30)];

    //初始化,接收一个视图的实例,btn1 frame x.y值无作用

    UIBarButtonItem * item3 = [[UIBarButtonItem alloc] initWithCustomView:btn1];

    

   //数组中的元素在导航条中由右到左显示

    NSArray *itemArray = [NSArray arrayWithObjects:item2,item3, nil];

    //self.navigationItem.rightBarButtonItem = itemArray;

    self.navigationItem.rightBarButtonItems = itemArray;


    //navigationBar的设置

    

    //导航条默认的高度为44,宽度为32

    //设置导航条的样式

    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;

    //只有导航条设置为黑色半透明风格时,背景颜色赋值才会生效 (UIBarMetrics) 设置iPhone的模式(竖屏叫人像模式,风景模式)


    self.navigationController.navigationBar.backgroundColor = [UIColor redColor];

    

//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"3.png"] forBarMetrics:UIBarMetricsDefault];

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"3.png"] forBarMetrics:UIBarMetricsDefault];

    

    //导航条的自动裁剪,将图片超出部分进行裁剪

    self.navigationController.navigationBar.clipsToBounds = YES;

    

    //隐藏导航栏的方法,导航条被隐藏后,视图控制器的视图会向上进行延展

    //self.navigationController.navigationBarHidden = NO;

    [self.navigationController setNavigationBarHidden:YES animated:YES];


}

-(void)itemClicked:(UIBarButtonItem *)item

{

    [self.view setBackgroundColor:[UIColor greenColor]];

}


//对UIButton btnClicked的处理事件

-(void)btnClicked:(UIButton *)btn

{

    //导航控制器对视图控制器的实例执行入栈操作(将视图控制器的实例加到导航控制器的栈中)

    ViewController2  *vc2 = [[ViewController2alloc]init];

    //取到导航控制器的实例(navController)

    //导航控制器对视图控制器执行入栈操作的一个方法,参数为视图控制器的实例,animated

    [self.navigationControllerpushViewController:vc2animated:YES];

}

zai"ViewController2.m"

-(void)viewDidLoad

{

     self.view.backgroundColor = [UIColor blueColor];

}


添加ViewController3类

#import "ViewController2.m"

加上头文件#import "ViewController3.h"

-(void)viewDidLoad

{

    [self.navigationItem setTitle:@"View2"];

    self.view.backgroundColor = [UIColor blueColor];

    

    

    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(itemClicked:)];

    

    self.navigationItem.leftBarButtonItem = backItem;

//在第二个界面中把第一个界面中导航条没有显示的显示出来

    [self.navigationController setNavigationBarHidden:NO animated:YES];


 UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [btn setTitle:@"push to vc3"forState:UIControlStateNormal];

    [btn setFrame:CGRectMake(10,100,300, 50)];

    [btn addTarget:selfaction:@selector(btnClicked:)forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    UIButton *popBtn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [popBtn setTitle:@"popvc2"forState:UIControlStateNormal];

    [popBtn setFrame:CGRectMake(10,150,300, 50)];

    [popBtn addTarget:selfaction:@selector(popBtnClicked:)forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:popBtn];

}

-(void)itemClicked:(UIBarButtonItem *)item

{

    

}

-(void)popBtnClicked:(UIButton *)popBtn

{

    //导航控制器对vc2执行出栈操作

    //将导航控制器中处于栈顶的视图控制器实例移除

    [self.navigationControllerpopViewControllerAnimated:YES];

}

-(void)btnClicked:(UIButton *)btn

{

    //导航控制器对视图控制器的实例执行入栈操作(将视图控制器的实例加到导航控制器的栈中)

    ViewController3  *vc3 = [[ViewController3alloc]init];

    //取到导航控制器的实例(navController)

    //导航控制器对视图控制器执行入栈操作的一个方法,参数为视图控制器的实例,animated

    [self.navigationControllerpushViewController:vc3animated:YES];

}


添加ViewController4类

#import "ViewController3.m"

加上头文件#import "ViewController4.h"

- (void)viewDidLoad

{

    [superviewDidLoad];

    self.view.backgroundColor = [UIColoryellowColor];

    

    UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [btn setTitle:@"push to vc4"forState:UIControlStateNormal];

    [btn setFrame:CGRectMake(10,100,300, 50)];

    [btn addTarget:selfaction:@selector(btnClicked:)forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

// Do any additional setup after loading the view.

}

-(void)btnClicked:(UIButton *)btn

{

    //直接返回到根视图控制器

   // [self.navigationController popToRootViewControllerAnimated:YES];

    ViewController4 *vc4 = [[ViewController4alloc]init];

    [self.navigationControllerpushViewController:vc4animated:YES];

}

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#import "ViewController4.m"

- (void)viewDidLoad

{

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorpurpleColor];


    UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [btn setTitle:@"push to root"forState:UIControlStateNormal];

    [btn setFrame:CGRectMake(10,100,300, 50)];

    [btn addTarget:selfaction:@selector(btnClicked:)forControlEvents:UIControlEventTouchUpInside];

[self.viewaddSubview:btn];

    

    UIButton * btn1 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [btn1 setTitle:@"pop to vc3"forState:UIControlStateNormal];

    [btn1 setFrame:CGRectMake(30,150,300, 50)];

    [btn1 addTarget:selfaction:@selector(btn1Clicked:)forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn1];

    

// Do any additional setup after loading the view.

}

-(void)btn1Clicked:(UIButton *)btn

{

    //跳转到指定的视图控制器

    /*1.  ViewControllers 能拿到所有视图控制器的实例

     2.ViewControllers 视图控制器在数组中的顺序即为视图控制器在导航控制器栈中的顺序

     3.传递进去的视图控制器的实例必须是存在于视图控制器栈中

     */

  

    NSArray *array =self.navigationController.viewControllers;

    

//    ViewController2 *vc2 = [[ViewController2 alloc] init];

//    [self.navigationController popToViewController:vc2 animated:YES];

    

    [self.navigationControllerpopToViewController:[arrayobjectAtIndex:1]animated:YES];

}


-(void)btnClicked:(UIButton *)btn

{

    //直接返回到根视图控制器

    [self.navigationControllerpopToRootViewControllerAnimated:YES];

    //    ViewController4 *vc4 = [[ViewController4 alloc] init];

    //    [self.navigationController pushViewController:vc4 animated:YES];

}





版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

导航控制器Demo

  • 2015-03-12 15:05
  • 41KB
  • 下载

导航控制器

  • 2015-03-12 15:42
  • 41KB
  • 下载

遍历导航控制器

项目当中有时候会用到判断 当前控制器是哪个控制器跳转过来的 因此我们需要找到一个方法拿到 上一个导航控制器 我们可以通过遍历的方式来获取导航控制器中 控制器的元素 下面提供了两种方式遍历 for-in...

IOS-导航控制器storyboard

  • 2015-05-26 17:18
  • 430B
  • 下载

UITabBarController 标签导航控制器的使用

tabBar 标签导航控制器的显示代表(类似NavigationBar) tabBarItem 标签的单元(类似按钮) viewControllers ...

导航控制器和表视图(二)

  • 2012-07-16 19:12
  • 164KB
  • 下载

IOS_导航控制器storyboard

  • 2015-05-26 17:15
  • 430B
  • 下载

iOS Dev 初识 导航控制器

导航控制器让用户使用内置动画在视图之间顺利移动,我们无需编写历史记录控制,导航控制器会自动处理Back按钮事件,让用户"出栈".导航控制器提供了一个简单的菜单栏.我们可以向菜单栏中添加按钮或者其他更复...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)