UINavigationController

来源:互联网 发布:hid门禁软件 编辑:程序博客网 时间:2024/05/18 03:23

标题

UINavigationController的创建和应用

//头文件#import "MainViewController.h"先创建一个viewControllerMainViewController *mainVC = [[MainViewController alloc]init];//创建导航视图控制器UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:mainVC];//添加根视图控制器  self.window.rootViewController = naVC;//释放[naVC release];[mainVC release];

在视图控制器中添加控件 控件的加载内容不详述

//这里的button是为了实现点击跳转的方法[button setTitle:@"下一页" forState:UIControlStateNormal];[self.view addSubview:button];button.layer.borderWidth = 1;button.layer.cornerRadius = 10;[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

对UINavigationController进行设置

//加上一个标题self.title = @"猫眼电影";//外观进行设置//背景颜色的设置//不是所有的背景颜色都是backgrandColorself.navigationController.navigationBar.barTintColor = [UIColor grayColor];//为了防止坐标系被篡改,我们把bar从半透明设置成不透明,这样坐标系的原点会自动向下推64self.navigationController.navigationBar.translucent = NO;//标题设置(效果与self.title大致相同 至于功能有什么区别不太清楚)//self.navigationItem.title = @"鹰王电影";//创建左右两边的按钮self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(barButtonAction:)] autorelease];//可以重写左右两边的按钮并在按钮上添加自定义的图片//创建一个小buttonUIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];rightButton.frame = CGRectMake(0, 0, 40, 40);[rightButton setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];

实现按钮的点击事件 这里有两种跳转方法模态跳转和导航视图控制器的跳转(可以通过属性传值)

//用模态跳转下一页SecondViewController *secVC = [[SecondViewController alloc]init];[secVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];//completion是block系统的block不需要写方法[self presentViewController:secVC animated:YES completion:^{}];[secVC release];//////////////////////////////////////////////////////////////////////第二种方法 通过导航视图控制器进行跳转通过导航视图控制器进行跳转创建下一页对象SecondViewController *secVC = [[SecondViewController alloc]init];//通过.navigationController属性调出当前管理者[self.navigationController pushViewController:secVC animated:YES];//内存管理[secVC release];//下面的属性通过对象调用进行赋值并传值 属性需要在声明对象的.h文件中声明secVC.number = 100;secVC.str = self.myTextField.text;secVC.arr = @[@"杨林",@"刘山山"];

从后往前跳转 需要在被跳转页写方法

//这里的popTo属性是枚举型不同的情况需要不同的方法-(void)click:(UIButton *)button{//从后往前跳//跳到根视图[self.navigationController popToRootViewControllerAnimated:YES];//跳到跳转本视图的视图[self.navigationController popViewControllerAnimated:YES];
0 0