[iOS基礎控制項,ios基礎控制項

來源:互聯網
上載者:User

[iOS基礎控制項,ios基礎控制項
A.控制器的建立 控制器常見的建立方式有以下幾種
通過storyboard建立

直接建立

1 ViewController *vc = [[ViewController alloc] init];
     xib設定了class後,當xib的檔案名稱跟controller類名一樣的時候,用這個方法預設就會載入xib中的controller 指定xib檔案來建立
1 ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
 1.從storyboard中建立(1)建立一個Empty Application (不帶storyboard) (2)建立window並加到screen上
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {2     // 手動添加window到screen3     self.window = [[UIWindow alloc] init];4     self.window.frame = [[UIScreen mainScreen] bounds];5     self.window.backgroundColor = [UIColor grayColor];6     [self.window makeKeyAndVisible];7     return YES;8 }
 (3)建立一個storyboard,拖入一個controller     (4)取出storyboard(其實就是相當於xib)
1     // 2.取得stroyboard2     UIStoryboard *sb = [UIStoryboard storyboardWithName:@"mysb" bundle:nil];
 (5)設定storyboard上controller為rootViewController  有兩種方式取得storyboard上的controllera.直接使用入口controller,這個view的背景色是橄欖綠 設定storyboard中的ViewController的class為自訂的controller 
1     // 3.1直接使用InitialViewController2     self.window.rootViewController = [sb instantiateInitialViewController];
  b.使用ID設定ViewController的class  再拖入一個ViewController,設定view的背景色是黃色,設定ID是”vc2" 
1     // 4.使用ID取得controller, 設定rootViewController2     self.window.rootViewController = [sb instantiateViewControllerWithIdentifier:@"vc2"];
  完成的載入過程:
 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2     // 1.手動添加window到screen 3     self.window = [[UIWindow alloc] init]; 4     self.window.frame = [[UIScreen mainScreen] bounds]; 5     self.window.backgroundColor = [UIColor grayColor]; 6     [self.window makeKeyAndVisible]; 7     8     // 2.取得stroyboard 9     UIStoryboard *sb = [UIStoryboard storyboardWithName:@"mysb" bundle:nil];10    11     // 3.取出storyboar中的controller12     // 3.1直接使用InitialViewController13     ViewController *controller = [sb instantiateInitialViewController];14    15     // 3.2使用ID16     ViewController2 *controller2 = [sb instantiateViewControllerWithIdentifier:@"vc2"];17    18     // 4.設定rootViewController19     self.window.rootViewController = controller2;20    21     return YES;22 }
  總結:1.建立Single View Application的時候,項目會內建一個storyboard,其實就是做了上面的事情設定了Main storyboard 的檔案,就會自動載入storyboard 2.不同的controller類負責不同的介面的操作 2.直接建立(不詳述) 3.指定xib檔案建立在之前沒有storyboard的時候使用這種方法(1)建立一個controller (2)建立一個xib (3)在xib拖入兩個view,設定一些特徵標識,方便對比 (4)設定其中一個view為控制器的viewa.更改 File’s Owner 的class為自訂的controller b.設定controller的view (5)從xib載入controller,並把view顯示到window上
 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2     // 1.手動添加window到screen 3     self.window = [[UIWindow alloc] init]; 4     self.window.frame = [[UIScreen mainScreen] bounds]; 5     self.window.backgroundColor = [UIColor grayColor]; 6     [self.window makeKeyAndVisible]; 7   8     // 從xib載入控制器, 設定rootViewController 9     self.window.rootViewController = [[XibViewController alloc] initWithNibName:@"myx" bundle:nil];10     11     return YES;12 }
   總結:1.storyboard:(這裡使用ViewController2為rootViewController) xib:(使用view1作為顯示的view)   B.建立控制器的view控制器的view建立有多種方式,(按照優先順序進行建立,僅使用最優先的方式)
  • loadView代碼(controller實現loadView方法)
  • storyboard描述
  • xib
   1.通過loadView(1)建立一個controller、storyboard、xib (2)配置好storyboard和xib的class為自訂的controller(3)給storyboard和xib的view加上明顯的標誌  (4)在controller類中實現loadView( 當controller的view是空的時候,就會調用loadView
 1 // 載入view,這是消極式載入,當需要使用view而view是空的時候調用 2 - (void)loadView { 3     NSLog(@"loadView..."); 4     self.view = [[UIView alloc] init]; 5     self.view.frame = [[UIScreen mainScreen] bounds]; 6     UILabel *label = [[UILabel alloc] init]; 7     label.frame = CGRectMake(40, 40, 100, 100); 8     label.text = @"loadView"; 9     [self.view addSubview:label];10 }
 (5)在delegate中配置controller到window上a.配置storyboard的controller為rootViewController
 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2     // 配置window 3     self.window = [[UIWindow alloc] init]; 4     self.window.frame = [[UIScreen mainScreen] bounds]; 5     self.window.backgroundColor = [UIColor grayColor]; 6     7     // 配置storyboard中的controller為rootViewController 8     self.window.rootViewController = [[UIStoryboard storyboardWithName:@"test" bundle:nil] instantiateInitialViewController]; 9   10      // 配置xib中的controller為rootViewController,主要要使用帶有loadView的controller11 //    self.window.rootViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];12    13     // 顯示window14     [self.window makeKeyAndVisible];15     return YES;16 }17  
會發現沒有起作用 b.同樣,使用xib中的controller為rootViewController,只要loadView存在,也不會起作用
1      // 配置xib中的controller為rootViewController,主要要使用帶有loadView的controller2     self.window.rootViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
  總結:在配置rootViewController的時候,如果配置的controller中實現了loadView方法,就會覆蓋storyboard或xib中的view    

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.