//創建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;
}