//创建vc
RootViewController *rootVC = [[RootViewController alloc]init];
//根视图
self.window.rootViewController = rootVC;
#pragma mark - 初始化
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
NSLog(@"初始化");
}
return self;
}
#pragma mark - 加载视图
-(void)loadView{
//重写时 一定要写super
//loadView方法 负责创建self.view
[super loadView];
NSLog(@"加载视图");
}
#pragma mark - 视图已经加载
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"视图已经加载");
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor lightGrayColor];
//UI导航控制器
//创建VC
ViewController *root = [[ViewController alloc] init];
//导航控制器: 管理控制器的控制器
//创建导航
UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:root];
//把导航设置为根视图
self.window.rootViewController = navi;
//导航栏设置:controller(栏)/item(栏上的元素)
//导航栏显示/隐藏
self.navigationController.navigationBarHidden = NO/YES;
//栏样式
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
//半透明效果
//开启效果时 屏幕左上角为坐标原点
//关闭时 导航栏的左下角为坐标原点
self.navigationController.navigationBar.translucent = YES;
//栏背景颜色
self.navigationController.navigationBar.backgroundColor = [UIColor yellowColor];
//栏颜色
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
//栏标题
// self.title = @"这是一个标题 ";
self.navigationItem.title = @"Back";
//分段
UISegmentedControl *seg = [[[UISegmentedControl alloc]initWithItems:@[@"消息",@"电话"]] autorelease];
seg.frame = CGRectMake(0, 0, 100, 30);
//栏标题视图
self.navigationItem.titleView = seg;
//栏左侧按钮
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(left:)] autorelease];
//栏右按钮
//系统按钮样式
UIBarButtonItem *b1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(right1)] autorelease];
//自定义按钮图片
UIBarButtonItem *b2 = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"gift.png"] style:UIBarButtonItemStylePlain target:self action:@selector(right2)] autorelease];
self.navigationItem.rightBarButtonItems = @[b1,b2];
//修改导航栏上内容的颜色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
//跳转页面
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(200, 200, 100, 100);
btn.backgroundColor = [UIColor yellowColor];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(goTwo) forControlEvents:UIControlEventTouchUpInside];
#pragma mark - 跳转页面
-(void)goTwo{
//1.获取第二页面对象
TwoViewController *twoVC = [[TwoViewController alloc] init];
//2.跳转(由导航控制器 从当前push到第二页)
[self.navigationController pushViewController:twoVC animated:YES];
//3.内存管理
[twoVC release];
}
//UI页面间传值
#warning 属性1: 在第二页声明一个属性 用来保存数据
@property(nonatomic,copy)NSString *string;
#warning 属性2: 在push页面之前传值(创建对象之后 push之前)
twoVC.string = self.tf1.text;
#warning 属性3: 通过属性给当前页内容赋值
self.tf2.text = self.string;
#warning 协议1: 声明协议(定义一个带参数的方法)
@protocol PassDelegate
#warning 协议2: 定义代理人属性
@property(nonatomic,assign)iddelegate;
#warning 协议3: 返回上一页之前 让代理人调用协议方法
[self.delegate passValue:self.tf2.text];
[self.navigationController popViewControllerAnimated:YES];
#warning 协议4: 签协议
@interface RootViewController ()
#warning 协议5: 设置代理人
//为了保证设置代理人的对象和push的对象是一个 在创建push对象之前 设置delegate.
twoVC.delegate = self;
[self.navigationController pushViewController:twoVC animated:YES];
[twoVC release];
}
#warning 协议6: 实现协议方法
-(void)passValue:(NSString *)string{
//把收到的string 赋值给输入框
self.tf1.text = string;
}