IOS creates controllers in a variety of ways _ios

Source: Internet
Author: User

This culture and education you through storyboard to create a controller, I hope to help you learn.

1. How to create the controller by storyboard, before the system load storyboard, help us to create a good controller.

By Uistoryboard This object, you can load the storyboard file Note: There must be storyboard, creating Uistoryboard objects makes sense, alloc init creating Uistoryboard objects is meaningless

1.1> Instantiateinitialviewcontroller: Controller that the default load arrow points to

1.2> Instantiateviewcontrollerwithidentifier: Find controller in Storyboard according to identity, and create. (The identity can not be transmitted randomly, will be an error, must storyboard have this logo.) )

1.3> the controller you are creating now cannot handle events, and if you need to handle events, you need to customize the controller.

Why can't the controller you're creating handle events?

Cause: When the controller in storyboard is found by Storyboardid, the controller object described in storyboard is created, the object of which is first known, and the class name is determined according to the custom class in storyboard. Then create the object, the default is the system with the Controller object, the system itself is not able to handle events. He can't write a listening method.

Therefore, to handle controller events, you must customize the controller.

2. How to imitate the system through the Main.storyboard loading program. (Items with Storyboard)

2.1> with Storyboard program, the default program one boot, will load storyboard, automatically create a good window and root controller.

2.2> system How to determine the need to load storyboard? Mainly to see if the main interface is set up, if set main, will go to load Main.storyboard.

2.3> in a new storyboard file, how to load the new storyboard, modify the main interface.

The 2.4> mimic program loads the Main.storyboard file as soon as it starts, creates a window, creates an arrow to point to the window root controller, and displays the window. (First clear the main interface settings, you need to delete the cache)

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {
  Override point for customization after application launch.
   
  Create a window (the window cannot be freed, otherwise it cannot be displayed)
  Self.window = [[UIWindow alloc]initwithframe:[uiscreen mainscreen].bounds];
   
  Load Storyboard,nil identity [nsbundle Mainbundle]
  uistoryboard *storyboard = [Uistoryboard storyboardwithname:@ ' Main ' Bundle:nil];
   
  The load arrow points to the controller
  uiviewcontroller *VC = [Storyboard Instantiateinitialviewcontroller];
 
  Let the controller become the root controller of the window
  self.window.rootViewController = VC;
   
  Display window
  [Self.window makekeyandvisible];
   
  Return YES
}

3. Create a controller view (empty project) via Xib (Xib first)

3.1 Xib Note points:
1> Xib must have a view that describes the view of the controller, because the controller's view property must have a value.
2> xib need to specify which controller to describe, describe uiview not needed, because Xib can describe a lot of uiview, not fixed dead, but the controller is not the same, one is used to describe only a controller.
3> Xib There may be a lot of view, you need to drag the line to indicate which is the controller view

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {
  Override point for customization after application launch.
   
  Create window
  Self.window = [[UIWindow alloc] Initwithframe:[uiscreen mainscreen].bounds]; 
   
  Create controller Viewcontroller via xib
  *VC = [[Viewcontroller alloc] initwithnibname:@ "Viewcontroller" bundle:nil]; 
  Self.window.rootViewController = VC;
   
  [Self.window makekeyandvisible];
    
  Return YES
}

The difference between 3.2 xib and storyboard storyboard has specified the controller's view, does not need our control, Xib needs us to manage manually.

The above is the entire content of this article, I hope to help you easily create an iOS controller is helpful.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.