IOS:UI系列之UINAVIGATIONCONTROLLER
又到了總結的時間了,突然間感覺時間過得好快啊, 總覺的時間不夠用,但是這也沒辦法啊, 只有自己擠時間了,雖然是零基礎,但是這並不能代表什麼啦,只要努力,收穫總還是有的, 同時我也相信廣大的博友肯定也有同樣的感觸吧! 接下來就讓我來為大家解讀我們今天所學習的內容吧,嘿嘿. 首先在上課剛開始時間,我們做了短短的練習, 對以往的知識有了進一步的瞭解, 也提高了熟練度, 但是時間總是很快的, 馬上我們就迎來了我們今天學習的新內容UINavigationControl! 首先讓我來介紹下UINavigationControl的建立方法吧: 1.建立導航控制器, 並指定導航控制器的RootViewController 2.設定導航控制器為rootViewController 具體建立過程我就用代碼來告訴大家吧,如下: FirstViewController *firstVC = [[FirstViewController alloc] init]; NSLog(@"第一個頁面初始化完成"); UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:firstVC]; [firstVC release]; NSLog(@"設定第一個頁面為導航控制器的rootViewController"); //步驟2.設定導航控制器為window的rootViewController self.window.rootViewController = navC; NSLog(@"設定導航控制器為window的RootViewController"); [navC release]; UINavigationControl,當行控制器, 繼承於UIViewController, 視圖控制器的視圖控制器, 用於管理伊利咧視圖控制器,被管理的視圖控制器以棧(先進後出, 後進先出)的形式儲存 每一個導航控制器都內建一個navigationBar(導航條), UINavigationBar繼承於UIView, navigationBar是用於管理導航條的展現(iOS7之前是不透明的,之後就變為透明的啦) 同時我們也可以對導航條以及上面的東西進行設定, 具體方法如下: //設定導航條是否半透明, 設定背景色,半透明失效 naviVC.navigationBar.translucent = YES; //設定導航條上item的顏色 naviVC.navigationBar.tintColor = [UIColor purpleColor]; //設定導航條樣式 naviVC.navigationBar.barStyle = UIBarStyleBlack; 在之後我們便進入了本節課最為重要的了 那就是傳值問題, 在今天我們學習的方法中由屬性傳值和單例傳值. 單例傳值的建立方法為: 1.類方法 2.傳回值類型是當前類 3.方法名:default + 類名 下面為大家帶上今天的例子: 下面圖示為1 2 3步 - (void)viewDidLoad{ [super viewDidLoad]; self.view.backgroundColor = [UIColor purpleColor]; // Do any additional setup after loading the view. self.navigationItem.title = @"首頁"; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 60, 30)]; label.text = @"使用者名稱"; label.textAlignment = NSTextAlignmentRight; [self.view addSubview:label]; [label release]; UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 150, 60, 30)]; label1.text = @"性別"; label1.textAlignment = NSTextAlignmentRight; [self.view addSubview:label1]; [label1 release]; field = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 30)]; field.borderStyle = UITextBorderStyleRoundedRect; field.placeholder = @"請輸入使用者名稱"; field.autocorrectionType = UITextAutocorrectionTypeNo; field.spellCheckingType = UITextSpellCheckingTypeNo; [self.view addSubview:field]; [field release]; field1 = [[UITextField alloc] initWithFrame:CGRectMake(100, 150, 200, 30)]; field1.borderStyle = UITextBorderStyleRoundedRect; field1.placeholder = @"請輸入性別"; [self.view addSubview:field1]; [field1 release]; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(70, 200, 70, 30); [button addTarget:self action:@selector(changPage:) forControlEvents:UIControlEventTouchUpInside]; [button setTitle:@"登陸" forState:UIControlStateNormal]; [self.view addSubview:button]; UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem]; button1.frame = CGRectMake(180, 200, 70, 30); [button1 addTarget:self action:@selector(changPage:) forControlEvents:UIControlEventTouchUpInside]; [button1 setTitle:@"取消" forState:UIControlStateNormal]; [self.view addSubview:button1]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(returnThekeyBorld:)]; [self.view addGestureRecognizer:tap]; [tap release]; //建立單例// Single *single = [Single defaultSingle];// NSLog(@"%@", single);// Single *single1 = [Single defaultSingle];// NSLog(@"%@", single1);// Single *single2 = [Single defaultSingle];// NSLog(@"%@", single2);} - (void)returnThekeyBorld:(UITapGestureRecognizer *)tap{ for (UIView *view in self.view.subviews) { if ([view class] == [UITextField class]) { [view resignFirstResponder]; } }} - (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.} - (void)changPage:(UIButton *)button{ Single *single = [Single defaultSingle]; single.nameString = field.text; NSLog(@"%@", single.nameString); HomeDetailViewController *detail = [[HomeDetailViewController alloc] init]; detail.nameString = [NSString stringWithFormat:@"歡迎%@ 性別%@登陸成功", field.text, field1.text]; [self.navigationController pushViewController:detail animated:YES]; [detail release]; } - (void)viewDidLoad{ self.view.backgroundColor = [UIColor redColor]; [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationItem.title = @"詳情頁"; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 200, 100)]; label.numberOfLines = 0; label.center = self.view.center;// label.text = _nameString; Single *single = [Single defaultSingle]; label.text = single.nameString; label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:label]; [label release];} #import <Foundation/Foundation.h> @interface Single : NSObject @property (nonatomic, retain) NSString *nameString, *genderString; //單例的建立方法//1.類方法//2.傳回值類型是當前類//3.方法名字:default + 類名+ (Single *)defaultSingle; #import "Single.h" @implementation Single + (Single *)defaultSingle{ static Single *single = nil; if (single == nil) { single = [[Single alloc] init]; } return single;} - (void)dealloc{ self.nameString = nil; self.genderString = nil; [super dealloc];}