IOS 7 Study - UIViewController

本文详细介绍了如何在iOS应用中使用UIViewController类来管理和切换不同的视图。通过创建一个空的应用模板,然后在Xcode中新建一个UIViewController子类,不使用.xib文件,直接在代码中初始化并设置为窗口的根视图控制器,实现了一个简单的白色视图展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Presenting and Managing Views with UIViewController

Problem
You want to switch among different views in your application.


Solution
Use the UIViewController class.

 

(

Apple’s strategy for iOS development was to use the model-view-controller (MVC) division
of labor.

Views are what get displayed to users, while the model is the data that
the app manages, or the engine of the app.

The controller is the bridge between the model and the view.

The controller, or in this case, the view controller, manages the relationship between the view and the model.

)

 

creating a view controller without a .xib file

 

1. created an application using the Empty Application template in Xcode

2. create a new view controller for your app

     a) In Xcode, select the File menu and then choose New → New File...

     b) In the New File dialog, make sure iOS is the selected category on the left and that
           Cocoa Touch is the chosen subcategory. Once you’ve done that, select the New
           Objective-C class

 

    c) On the next screen, make sure that the “Subclass of ” the text field says UIView
         Controller. Also make sure that neither the “Targeted for iPad” nor the “With XIB
         for user interface” checkboxes is selected

 

 

    d) Save your controller

 

   e) Now find the application:didFinishLaunchingWithOptions: method of the app
       delegate and instantiate the view controller and set it as the root view controller of
       your window

- (BOOL) application:(UIApplication *)application
 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  self.viewController 
    = [[ViewController alloc] initWithNibName:nil bundle:nil];

  self.window 
    = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

  /* Make our view controller the root view controller */
  self.window.rootViewController = self.viewController;

  // set the view background
  self.window.backgroundColor = [UIColor whiteColor];

  [self.window makeKeyAndVisible];

  return YES;
}

 

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.

 

if you had selected the “With XIB for user interface” checkbox, Xcode would have also generated a .xib file for you. In that
case, you can load your view controller from that .xib file by passing the .xib file’s name(without the extension)

 

self.viewController = [[ViewController alloc]
                               initWithNibName:@"ViewController"
                               bundle:nil];

 

 

转载于:https://www.cnblogs.com/davidgu/p/3543472.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值