tailieunhanh - iOS 5 Programming Cookbook phần 3

Tất cả những gì bạn thực sự cần phải làm bây giờ là để cung cấp nội dung mô hình dữ liệu (Model Controller) và bạn đi. Nếu bạn cần để tùy chỉnh màu sắc và hình ảnh trong các bộ điều khiển xem của bạn, chỉ cần làm như vậy bằng cách sử dụng giao diện Builder để sửa đổi các tập tin. | synthesize window _window synthesize rootViewController 3. Now find the application didFinishLaunchingWithOptions method of the app delegate inside the implementation .m file and instantiate the view controller and add it to our window - BOOL application UIApplication application didFinishLaunchingWithOptions NSDictionary launchOptions UIWindow alloc initWithFrame UIScreen mainScreen bounds makeKeyAndVisible RootViewController alloc initWithNibName nil bundle NULL addSubview return YES We add the view of the view controller to the window not the view controller itself. Now if you run your app on the simulator you will see a black screen. The reason is that our view controller s view doesn t have a background color yet so go to the file and find the viewDidLoad method which is in a state similar to this - void viewDidLoad super viewDidLoad Remove the comment lines from around this method - void viewDidLoad super viewDidLoad Now let s set the background color of our view controller s view to white - void viewDidLoad super viewDidLoad 162 Chapter 2 Implementing Controllers and Views UlColor whiteColor Go ahead and run the app on the simulator. You will now see a plain white view on the screen. Congratulations you just created a view controller and now you have access to the view controller and its view object. While creating the view controller Figure 2-26 if you had selected the With XIB for user interface checkbox then Xcode would have also generated an .xib file for you. In such case we would have to load our view controller from that .xib file by passing the .xib file s full name to the initWithNibName parameter of the initWithNibName bun dle method of the view controller like so - BOOL application UIApplication application didFinishLaunchingWithOptions NSDictionary launchOptions UIWindow alloc initWithFrame UIScreen