iOS UI07_導航視圖控制器
//// MainViewController.m// UI07_導航視圖控制器//// Created by dllo on 15/8/6.// Copyright (c) 2015年 zhozhicheng. All rights reserved.//#import MainViewController.h#import SecondViewController.h@interface MainViewController ()@property(nonatomic,retain)UITextField *textfield;@end@implementation MainViewController-(void)dealloc{ [_textfield release]; [super dealloc];}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor=[UIColor cyanColor]; //導航視圖控制器高度44,上面的狀態列是20,加在一起預設是64 UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem]; button.frame=CGRectMake(200, 100, 100, 40); [button setTitle:@下一頁 forState:UIControlStateNormal]; button.layer.borderWidth=1; button.layer.cornerRadius=10; [self.view addSubview:button]; [button addTarget:self action:@selector(click:) forControlEvents: UIControlEventTouchUpInside]; //對導航視圖控制器進行設定 //加上一個標題// self.title =@貓眼電影; //外觀設定 //背景顏色,不是所有的背景顏色都是backgroundColor// self.navigationController.navigationBar.barTintColor=[UIColor greenColor]; //建立一個UIview UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; view.backgroundColor=[UIColor blackColor]; [self.view addSubview:view]; [view release]; //為了防止座標系被篡改,我們把bar從半透明設定成不透明,這樣座標系的原點會自動向下推64 self.navigationController.navigationBar.translucent=NO; //設定裡面的內容 //標題設定 self.navigationItem.title=@鷹王電影; //指定一些視圖,稱為titleView UISegmentedControl *seg=[[UISegmentedControl alloc] initWithItems:@[@資訊,@通話]]; self.navigationItem.titleView=seg; //建立左右兩邊的按鈕 self.navigationItem.leftBarButtonItem=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leftButtonAction:)] autorelease];// self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@天平.png] style:UIBarButtonItemStylePlain target:self action:@selector(right:)]; //建立一個小button UIButton *rightButton=[UIButton buttonWithType:UIButtonTypeCustom]; rightButton.frame=CGRectMake(0, 0, 40, 40); [rightButton setImage:[UIImage imageNamed:@天平.png] forState:UIControlStateNormal]; self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:rightButton]; self.textfield=[[UITextField alloc] initWithFrame:CGRectMake(100, 200, 100, 40)]; self.textfield.layer.borderWidth=1; self.textfield.layer.cornerRadius=10; [self.view addSubview:self.textfield]; [self.textfield release];}-(void)leftButtonAction:(UIBarButtonItem *)button{}-(void)right:(UIBarButtonItem *)button{}-(void)click:(UIButton *)button{// //用模態跳轉下一頁// SecondViewController *secondVC=[[SecondViewController alloc] init];// [secondVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];// [self presentViewController:secondVC animated:YES completion:^{// // }]; //用導航視圖控制器Navigation進行跳轉 //先建立下一頁對象 SecondViewController *secVC=[[SecondViewController alloc] init]; //屬性傳值第二步 secVC.number=100; secVC.str=self.textfield.text; secVC.arr=@[@楊林,@劉山山]; // [self.navigationController pushViewController:secVC animated:YES]; //記憶體管理 [secVC release];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end
//// SecondViewController.h// UI07_導航視圖控制器//// Created by dllo on 15/8/6.// Copyright (c) 2015年 zhozhicheng. All rights reserved.//#import @interface SecondViewController : UIViewController//屬性傳值第一步,在第二個頁面寫一條屬性@property(nonatomic,assign)NSInteger number;//針對字串寫一條屬性@property(nonatomic,copy)NSString *str;@property(nonatomic,retain)NSArray *arr;@end
//// SecondViewController.m// UI07_導航視圖控制器//// Created by dllo on 15/8/6.// Copyright (c) 2015年 zhozhicheng. All rights reserved.//#import SecondViewController.h#import ThirdViewController.h@interface SecondViewController ()@property(nonatomic,retain)UILabel *label;@end@implementation SecondViewController-(void)dealloc{ [_label release]; [super dealloc]; [_arr release]; [_str release];}- (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(200, 100, 100, 40); [button setTitle:@下一頁 forState:UIControlStateNormal]; button.layer.borderWidth=1; button.layer.cornerRadius=10; [self.view addSubview:button]; [button addTarget:self action:@selector(click:) forControlEvents: UIControlEventTouchUpInside]; NSLog(@%ld,self.number); self.label=[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 50, 40)]; self.label.backgroundColor=[UIColor redColor]; [self.view addSubview:self.label]; [self.label release]; self.label.text=self.str; NSLog(@%@,self.arr[0]);}-(void)click:(UIButton *)button{ ThirdViewController *thirdVC=[[ThirdViewController alloc] init]; [self.navigationController pushViewController:thirdVC animated:YES]; [thirdVC release];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end
//// ThirdViewController.m// UI07_導航視圖控制器//// Created by dllo on 15/8/6.// Copyright (c) 2015年 zhozhicheng. All rights reserved.//#import ThirdViewController.h@interface ThirdViewController ()@end@implementation ThirdViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor=[UIColor yellowColor]; UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem]; button.frame=CGRectMake(200, 100, 100, 40); [button setTitle:@返回 forState:UIControlStateNormal]; button.layer.borderWidth=1; button.layer.cornerRadius=10; [self.view addSubview:button]; [button addTarget:self action:@selector(click:) forControlEvents: UIControlEventTouchUpInside];}-(void)click:(UIButton *)button{ //從後往前跳 [self.navigationController popToRootViewControllerAnimated:YES ]; //跳到上一頁// [self.navigationController popViewControllerAnimated:YES];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end