IOS 7 Study - UIViewController

简介: Presenting and Managing Views with UIViewController ProblemYou want to switch among different views in your application.

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];

 

 

目录
相关文章
|
iOS开发
iOS - UIViewController生命周期(storyboard/Xib/纯代码)(下)
iOS - UIViewController生命周期(storyboard/Xib/纯代码)
|
iOS开发
iOS - UIViewController生命周期(storyboard/Xib/纯代码)(上)
iOS - UIViewController生命周期(storyboard/Xib/纯代码)
|
iOS开发
iOS对UIViewController生命周期和属性方法的解析(二)
iOS对UIViewController生命周期和属性方法的解析
213 0
iOS对UIViewController生命周期和属性方法的解析(二)
|
设计模式 前端开发 iOS开发
iOS对UIViewController生命周期和属性方法的解析(一)
iOS对UIViewController生命周期和属性方法的解析
233 0
iOS对UIViewController生命周期和属性方法的解析(一)
|
iOS开发
【iOS】UIViewController基类的实现
继承是面向对象编程语言的三大特性之一,写好基类会给App的开发带来极大的方便。在iOS开发中,一般一个页面就对应一个ViewController,ViewController在开发中用的也很多,写一个好的ViewController的基类,会让开发变得轻松很多。
1732 0
|
iOS开发
iOS开发之UIView与UIViewController的生命周期总结
iOS开发中,创建View常见的两种方式一个是纯代码,一个是借助于XIB;创建ViewController常见的也有两种方式一个是纯代码,一个是借助于StoryBoard。
1267 0
|
iOS开发 Swift 缓存
iOS - UIViewController
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIViewController : UIResponder @available(iOS 2.0, *) public class UIViewController : UIResponder, NSCoding, UIAppearanceContainer, UITraitEnvironment, UIContentContainer, UIFocusEnvironment 视图控制器负责页面的创建、事件处理等。
1424 0
|
1月前
|
Java Android开发 Swift
安卓与iOS开发对比:平台选择对项目成功的影响
【10月更文挑战第4天】在移动应用开发的世界中,选择合适的平台是至关重要的。本文将深入探讨安卓和iOS两大主流平台的开发环境、用户基础、市场份额和开发成本等方面的差异,并分析这些差异如何影响项目的最终成果。通过比较这两个平台的优势与挑战,开发者可以更好地决定哪个平台更适合他们的项目需求。
110 1
|
7天前
|
安全 数据处理 Swift
深入探索iOS开发中的Swift语言特性
本文旨在为开发者提供对Swift语言在iOS平台开发的深度理解,涵盖从基础语法到高级特性的全面分析。通过具体案例和代码示例,揭示Swift如何简化编程过程、提高代码效率,并促进iOS应用的创新。文章不仅适合初学者作为入门指南,也适合有经验的开发者深化对Swift语言的认识。
25 9
|
7天前
|
Android开发 Swift iOS开发
探索安卓与iOS开发的差异和挑战
【10月更文挑战第37天】在移动应用开发的广阔舞台上,安卓和iOS这两大操作系统扮演着主角。它们各自拥有独特的特性、优势以及面临的开发挑战。本文将深入探讨这两个平台在开发过程中的主要差异,从编程语言到用户界面设计,再到市场分布的不同影响,旨在为开发者提供一个全面的视角,帮助他们更好地理解并应对在不同平台上进行应用开发时可能遇到的难题和机遇。