#pragma mark invoke after application loaded- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ NSLog(@"載入完畢..."); //init window self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. // 傳入xib檔案名稱來init controller self.viewController = [[[PPSViewController alloc] initWithNibName:@"PPSViewController" bundle:nil] autorelease]; // setting the root view of Window self.window.rootViewController = self.viewController; // 下面的代碼和上面一條設定跟view效果一樣 // [self.window addSubview:self.viewController.view]; [self.window makeKeyAndVisible]; //view可見 return YES;}
同時在根視圖上面,我們還可以添加子視圖。所有的控制項都會繼承UIView;
- (void)viewDidLoad{ [super viewDidLoad]; // 設定介面的背景 [self.view setBackgroundColor:[UIColor redColor]]; CGRect frame=CGRectMake(10, 170, 300, 50); // 使用指定的矩形進行建立標籤 UILabel *lable= [[UILabel alloc]initWithFrame:frame]; lable.text=@"Hello,我是被添加的子視圖"; lable.textColor=[UIColor redColor]; [self.view addSubview:lable];}
運行結果如下: