標籤:style blog io ar color os 使用 sp for
二十三:addSubview和insertSubview 區別
addSubview 是將view加到所有層的最頂層相當於將insertSubview的atIndex參數設定成view.subviews count 即[view addSubview:oneview] == [view insertSubview:oneview atIndex:view.subviews count]addSubview是加到最後insertSubview是加到指定的位置UIView* view=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 30.0, 50.0)]; view.backgroundColor=[UIColor redColor]; [self.view addSubview:view]; UIView* newview=[[UIView alloc] initWithFrame:CGRectMake(30, 0, 40, 50)]; newview.backgroundColor=[UIColor blueColor]; [self.view addSubview:newview]; UIView* twoview=[[UIView alloc] initWithFrame:CGRectMake(0, 50, 100, 200)]; twoview.backgroundColor=[UIColor yellowColor]; [self.view addSubview:twoview]; //第一個從0開始索引 UIView* aview=[[UIView alloc] initWithFrame:CGRectMake(20, 20, 120, 200)]; aview.backgroundColor=[UIColor grayColor]; [self.view insertSubview:aview atIndex:2];
二十四:loaded the "XXXView" nib but the view outlet was not set 解決方案
‘-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "XXXView" nib but the view outlet was not set.‘查書才知道,沒有做nib檔案到xxxViewControler程式的關聯,特此記錄下來:1, 開啟nib檔案2, 點擊"File‘s Owner", 按command+4,設定Class為xxxViewControler3, 按Control+"Files‘s Owner", 裡面有個預設的IBOutlet變數view, 看一下後面有沒有做關聯,如果沒有就拉到下面的View和視圖做個關聯
二十五:UIViewController和UIView關係
UIViewController是視圖控制器,UIView是視圖,UIViewController是來控制UIView的。UIViewController就像是一個容器,它可以對其內部的對象進行操作,UIView是容器中的對象,一個容器裡面可以有好多個物件;UIView工作在第一線,向使用者展示表現的內容,並接受使用者的互動。UIViewController相當於導演,按照計劃編排屬下的UIView以何種形式展現,其中包含一些花哨的技巧,如翻轉、淡入淡出等。 UIVewController的另一個功能是處理使用者互動操作,注意這裡我說的是"處理",當然UIViewController本身是不知道使用者互動的,這就需要UIView將使用者的互動操作(例如:touchesBegintouchesMoved)傳遞上來。一般常用的兩種方法完成這種傳遞: 1、[self nextResponder] touchesBegin:touches... 2、使用Notification 不管以何種方式,如果UIViewController得到了使用者的輸入,那麼它應該對UIView做些改變以響應使用者的輸入,這裡要注意UIViewController應該只改變UIView的表現,而不應該處理其它事情,更多的操作通過Delegate來進行,關於Delegate的應用場合下次講解訊息的傳遞方式中一起闡述。
二十六:頁面傳值問題
假如要從A傳到B頁,可以很簡單直接在B中定義一個屬性,然後在頁面跳轉前給它賦值,實現跳轉;UserInfo* userModel=[[UserInfo alloc] init]; userModel.userName=self.textBox1.text; userModel.age=25; userModel.sex=YES; //TwoController* twocroller=[[TwoController alloc]init]; 別這麼寫,或者傳過去為空白,因為視圖間沒有什麼聯絡 TwoController* twocroller= [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"TwoController"]; //把B頁的視圖定義一個標識TwoControllertwocroller.newuserinfo = userModel; [self presentViewController:twocroller animated:YES completion:nil];如果從A跳到B頁,然後再從B頁傳值給A頁,就可以使用委託的方式;
二十七:集合視圖跟表視圖
集合視圖跟表視圖的一些概念是不一樣,比如節;實現委託的方法也是不一樣;
二十八:通過Segue標識進行跳轉
[self performSegueWithIdentifier:@"twoSegue" sender:self];
其中push不能直接從ViewController跳轉到ViewController;
二十九:UIScreen(螢幕)、UIWindow(畫框)、UIView(畫布)、didFinishLaunchingWithOptions的概念
//didFinishLaunchingWithOptions 方法:顧名思義。在app開始運行時會調用裡面的方法。- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ //返回的是帶有狀態列的矩形 self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; CGRect bound = [[UIScreen mainScreen]bounds]; //返回的是帶有狀態列的Rect NSLog(@"boundwith:%f boundheight:%f",bound.size.width,bound.size.height); NSLog(@"boundx:%f boundy:%f",bound.origin.x,bound.origin.y); //2012-08-03 23:21:45.716 DinkMixer[599:c07] boundwith:320.000000 boundheight:480.000000 //2012-08-03 23:21:45.719 DinkMixer[599:c07] boundx:0.000000 boundy:0.000000 CGRect appBound = [[UIScreen mainScreen]applicationFrame]; //返回的是不帶有狀態列的Rect NSLog(@"appBoundwith:%f boundheight:%f",appBound.size.width,appBound.size.height); NSLog(@"appBoundx:%f boundy:%f",appBound.origin.x,appBound.origin.y); //2012-08-03 23:21:45.720 DinkMixer[599:c07] appBoundwith:320.000000 boundheight:460.000000 //2012-08-03 23:21:45.720 DinkMixer[599:c07] appBoundx:0.000000 boundy:20.000000 //很明顯狀態列佔用了空間20像素 MasterViewController *masterViewController = [[[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil] autorelease];//根據nib檔案的名稱來建立一個視圖控制器 self.navigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];//建立一個導航控制器,並指定該導航控制器的根視圖控制器為上面建立的masterViewController self.window.rootViewController = self.navigationController;//表單(window)有一個根視圖控制器——這個視圖控制器負責配置當表單顯示時最先顯示的視圖。要讓你的視圖控制器的內容顯示在表單中,需要去設定表單的根視圖控制器為你的視圖控制器。 [self.window makeKeyAndVisible];//這行代碼會讓包含了視圖控制器視圖的Window視窗顯示在螢幕上。 return YES;}
IOS開發基礎知識--片段5