UI UINavigationController导航控制器
来源:互联网 发布:致命的自负 知乎 编辑:程序博客网 时间:2024/04/28 05:59
<span style="font-size:18px;">AppDelegate.m</span>
<span style="font-size:18px;"> self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. Root_ViewController *rootVC=[[Root_ViewController alloc]init]; UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:rootVC]; //设置返回按钮的颜色 nav.navigationBar.tintColor=[UIColor magentaColor]; //设置导航条的颜色// nav.navigationBar.barTintColor=[UIColor yellowColor]; //设置导航条navigationBar是否透明,默认是YES nav.navigationBar.translucent=YES; //设置navigationBar的样式 nav.navigationBar.barStyle=UIBarStyleBlack; //设置navigationBar的背景图片 [nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"1.png"] forBarMetrics:UIBarMetricsDefault]; self.window.rootViewController=nav; [rootVC release]; [nav release]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; /* UINavigationController是导航视图控制器,负责管理具有层次结构的视图,是管理控制器的控制器,其内容分为两部分:一部分是导航条(navigationBar),另外一部分是内容(contentView)。 导航视图控制器维持了一个栈结构,当push到另外一个viewController时,会执行入栈操作;当返回上一个viewController或返回指定viewController时,会执行出栈操作。 UINavigationBar:导航条,继承自UIView,在MVC中位于V层,负责标题,按钮的显示。其内容也维持了一个栈结构,当push页面时,navigationBar会执行入栈操作,navigationBar会将viewController中的navigationItem属性取出,放入栈中,并显示navigationItem中的相关信息;pop页面时,出栈,将navigationItem从栈中移除,并移除显示的按钮,标题等 UINavigationItem :UIViewController的属性,主要用于为navigationBar提供数据,在MVC中,属于M层,类似于一个包裹,内部存放了导航条上显示的按钮,标题等信息。当显示该ViewController时,navigationItem内部信息会被navigationBar显示 UIBarButtonItem:封装了NavigationBar上的按钮信息。在MVC中属于M层(不继承UIView)。NavigationBar在拿到navigationItem后,会将里面的BarButtonItem取出,根据BarButtonItem创建UIButton对象,显示在导航条上。 */ //UINavigationController继承于UIViewController,以栈的⽅式管理所控制的视图控制器,⾄少要有⼀个被管理的视图控制器,这个控制器我们称作,导航控制器的根视图控制器。 //任何继承⾃自UIViewController的类(多态)都可以作为根控制器 //navigationBar—导航条,iOS7之后默认是透明的,iOS7之前默认是不透明的。 navigationBar在透明情况,与contentView会重合⼀部分区域。navigationBar在不透明情况,contentView跟在navigationBar的下⾯。 //navigationBar竖屏下默认⾼度44,横屏下默认⾼度32.</span>
<span style="font-size:18px;">Root_ViewController.m</span>
<span style="font-size:18px;"></span><pre name="code" class="objc">- (void)viewDidLoad{ [super viewDidLoad]; self.view.backgroundColor=[UIColor cyanColor]; //设置navigationItem的文本 self.navigationItem.title=@"首页"; //设置navigationItem的文本视图 UIView *titleView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 44)]; titleView.backgroundColor=[UIColor magentaColor]; self.navigationItem.titleView=titleView; [titleView release]; //设置左按钮 UIBarButtonItem *leftBtn=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(leftBtnAction:)]; self.navigationItem.leftBarButtonItem=leftBtn; [leftBtn release]; //设置右按钮,将图片转换成镂空模式,图片才能显示,否则图片不能正常使用 UIImage *img=[[UIImage imageNamed:@"3.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; UIBarButtonItem *rightBtn=[[UIBarButtonItem alloc]initWithImage:img style:UIBarButtonItemStyleBordered target:self action:@selector(rightBtnAction:)]; self.navigationItem.rightBarButtonItem=rightBtn; [rightBtn release]; UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil]; UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:nil]; UIBarButtonItem *item3=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:nil]; NSArray *arr=[NSArray arrayWithObjects:item1,item2,item3, nil]; self.navigationItem.leftBarButtonItems=arr; UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem]; button.frame=CGRectMake(50, 150, 100, 40); button.backgroundColor=[UIColor redColor]; [button setTitle:@"下一页" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(50, 100, 100, 50)]; label.backgroundColor=[UIColor whiteColor]; [self.view addSubview:label]; [label release]; self.label=label; }- (void)leftBtnAction:(UIBarButtonItem*)leftBtn{ NSLog(@"leftBtnAction:");}- (void)rightBtnAction:(UIBarButtonItem *)rightBtn{ NSLog(@"rightBtnAction:");}- (void)buttonAction:(UIButton*)button{ NSLog(@"buttonAction:"); SecondViewController *secondVC=[[SecondViewController alloc]init]; secondVC.str=@"你好"; /* 属性传值:如果从A页面往B页面传值,在B页面中声明属性,在A页面的跳转事件中给B页面的属性赋值 */ //页面切换 [self.navigationController pushViewController:secondVC animated:YES]; [secondVC release]; }- (BOOL)textFieldShouldReturn:(UITextField *)textField{ NSLog(@"%@",textField.text); self.label.text=textField.text; return YES;}-(void)viewWillAppear:(BOOL)animated{ //打印navigationController的所有viewControllers NSLog(@"%@",self.navigationController.viewControllers);}-(void)dealloc{ [_label release]; [super dealloc];}
<span style="font-size:18px;"></span><pre name="code" class="objc"><span style="font-size:18px;">SecondViewController.m</span>
- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor=[UIColor orangeColor]; UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem]; button.frame=CGRectMake(50, 150, 100, 40); button.backgroundColor=[UIColor redColor]; [button setTitle:@"下一页" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(50, 100, 100, 50)]; label.text=self.str; label.font=[UIFont systemFontOfSize:36]; [self.view addSubview:label]; [label release]; self.label=label; UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 200, 200, 50)]; textField.backgroundColor=[UIColor whiteColor]; textField.delegate=[self.navigationController.viewControllers objectAtIndex:0]; [self.view addSubview:textField]; [textField release]; self.textField=textField; }- (void)buttonAction:(UIButton*)button{ NSLog(@"buttonAction:"); ThirdViewController *thirdVC=[[ThirdViewController alloc]init]; //页面切换 [self.navigationController pushViewController:thirdVC animated:YES]; [thirdVC release];}/* 从后一个页面返回前一个页面不会执行前页面的loadView,viewDidLoad方法,而是执行viewWillAppear方法 因为:loadView和viewDidLoad方法的作用是将视图加载到内存,而从后一个页面返回时,前一个页面已经加载到内存,所以不用再次加载,所以不执行loadView和viewDidLoad方法 */-(void)viewWillAppear:(BOOL)animated{ //打印navigationController的所有viewControllers NSLog(@"%@",self.navigationController.viewControllers); //在视图将要显示的时候给label的text赋值 self.label.text=self.str;}-(void)dealloc{ [_str release]; [_label release]; [_textField release]; [super dealloc];}
<span style="font-size:18px;"></span><pre name="code" class="objc"><span style="font-size:18px;">ThirdViewController.m</span>
- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor=[UIColor blueColor]; UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem]; button.frame=CGRectMake(50, 150, 100, 40); button.backgroundColor=[UIColor redColor]; [button setTitle:@"回到上一页" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; }- (void)buttonAction:(UIButton*)button{ SecondViewController *secondVC=[self.navigationController.viewControllers objectAtIndex:1]; secondVC.str=@"Hello"; ////返回上一页 [self.navigationController popViewControllerAnimated:YES]; //返回到指定下标的// UIViewController *vc=[self.navigationController.viewControllers objectAtIndex:0];// [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES]; //返回根视图控制器 //[self.navigationController popToRootViewControllerAnimated:YES];}-(void)viewWillAppear:(BOOL)animated{ //打印navigationController的所有viewControllers NSLog(@"%@",self.navigationController.viewControllers);}
0 0
- UI UINavigationController导航控制器
- UI:导航控制器UINavigationController的使用
- 导航控制器UINavigationController和选项卡栏控制器UI...
- UI课程 08 导航控制器 UINavigationController 、容器控制器
- Snail—UI学习之导航视图控制器UINavigationController(系统)
- iOS-UI-05 导航控制器的使用 UINavigationController
- 【UI初级--连载六】---------导航控制器(UINavigationController)
- 导航视图控制器UINavigationController
- 导航控制器 ( UINavigationController )
- 导航控制器UINavigationController
- UINavigationController导航控制器详解
- UINavigationController导航控制器…
- UINavigationController导航控制器…
- UINavigationController导航控制器…
- UINavigationController 导航控制器
- 导航控制器UINavigationController
- 导航控制器:UINavigationController
- 自定义UINavigationController(导航控制器)
- linux之目录权限
- TCP全连接队列和半连接队列已满之后的连接建立过程抓包分析
- HDU ACM 1029-Ignatius and the Princess IV
- 第15题
- 文件或文件夹删除
- UI UINavigationController导航控制器
- 正则的前向声明和反前向声明
- 黑马程序员—java集合框架整理
- 经典的汉诺塔问题
- jsp如何转发
- 理解Python中的with…as…语法
- [POJ] 2013 -> Symmetric Order
- 【HDU 4150】Powerful Incantation —— 字符串匹配
- 8-3 简要说明RFID系统的时隙ALOHA算法的工作过程