//創(chuàng)建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{
//重寫時(shí) 一定要寫super
//loadView方法 負(fù)責(zé)創(chuàng)建self.view
[super loadView];
NSLog(@"加載視圖");
}
#pragma mark - 視圖已經(jīng)加載
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"視圖已經(jīng)加載");
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor lightGrayColor];
//UI導(dǎo)航控制器
//創(chuàng)建VC
ViewController *root = [[ViewController alloc] init];
//導(dǎo)航控制器: 管理控制器的控制器
//創(chuàng)建導(dǎo)航
UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:root];
//把導(dǎo)航設(shè)置為根視圖
self.window.rootViewController = navi;
//導(dǎo)航欄設(shè)置:controller(欄)/item(欄上的元素)
//導(dǎo)航欄顯示/隱藏
self.navigationController.navigationBarHidden = NO/YES;
//欄樣式
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
//半透明效果
//開啟效果時(shí) 屏幕左上角為坐標(biāo)原點(diǎn)
//關(guān)閉時(shí) 導(dǎo)航欄的左下角為坐標(biāo)原點(diǎn)
self.navigationController.navigationBar.translucent = YES;
//欄背景顏色
self.navigationController.navigationBar.backgroundColor = [UIColor yellowColor];
//欄顏色
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
//欄標(biāo)題
// ? ?self.title = @"這是一個(gè)標(biāo)題 ";
self.navigationItem.title = @"Back";
//分段
UISegmentedControl *seg = [[[UISegmentedControl alloc]initWithItems:@[@"消息",@"電話"]] autorelease];
seg.frame = CGRectMake(0, 0, 100, 30);
//欄標(biāo)題視圖
self.navigationItem.titleView = seg;
//欄左側(cè)按鈕
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(left:)] autorelease];
//欄右按鈕
//系統(tǒng)按鈕樣式
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];
//修改導(dǎo)航欄上內(nèi)容的顏色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
//跳轉(zhuǎn)頁面
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 - 跳轉(zhuǎn)頁面
-(void)goTwo{
//1.獲取第二頁面對(duì)象
TwoViewController *twoVC = [[TwoViewController alloc] init];
//2.跳轉(zhuǎn)(由導(dǎo)航控制器 從當(dāng)前push到第二頁)
[self.navigationController pushViewController:twoVC animated:YES];
//3.內(nèi)存管理
[twoVC release];
}
//UI頁面間傳值
#warning 屬性1: 在第二頁聲明一個(gè)屬性 用來保存數(shù)據(jù)
@property(nonatomic,copy)NSString *string;
#warning 屬性2: 在push頁面之前傳值(創(chuàng)建對(duì)象之后 push之前)
twoVC.string = self.tf1.text;
#warning 屬性3: 通過屬性給當(dāng)前頁內(nèi)容賦值
self.tf2.text = self.string;
#warning 協(xié)議1: 聲明協(xié)議(定義一個(gè)帶參數(shù)的方法)
@protocol PassDelegate
#warning 協(xié)議2: 定義代理人屬性
@property(nonatomic,assign)iddelegate;
#warning 協(xié)議3: 返回上一頁之前 讓代理人調(diào)用協(xié)議方法
[self.delegate passValue:self.tf2.text];
[self.navigationController popViewControllerAnimated:YES];
#warning 協(xié)議4: 簽協(xié)議
@interface RootViewController ()
#warning 協(xié)議5: 設(shè)置代理人
//為了保證設(shè)置代理人的對(duì)象和push的對(duì)象是一個(gè) 在創(chuàng)建push對(duì)象之前 設(shè)置delegate.
twoVC.delegate = self;
[self.navigationController pushViewController:twoVC animated:YES];
[twoVC release];
}
#warning 協(xié)議6: 實(shí)現(xiàn)協(xié)議方法
-(void)passValue:(NSString *)string{
//把收到的string 賦值給輸入框
self.tf1.text = string;
}