iOS Learn how to create a 4_ controller and create a controller view

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:14px">UIScreen is a physical screen related to the device</span></p></p><p><p><span style="font-size:14px">Windows is a window corresponding to the UIWindow class, inherited from the Uiview,window to be displayed on screen must be set to the main window and Visible. next, you can add some controls to the UIWINDOW.</span></p></p><p><p><span style="font-size:14px">is simply a hierarchical relationship</span></p></p><p><p></p></p><p><p></p></p><p><p><span style="font-size:14px"><span style="color:#ff0000">Viewcontroller is used to organize and control the view, and the difference is that the view controller is used here viewcontroller, do not need to directly assign the view to the window, instead, only need to create a view controller for the window, The view controller automatically adds his view to the WINDOW. </span>as shown in the Following:</span></p></p><p><p><span style="font-size:14px">The introduction of the controller is a bit like activity inside ANDROID.</span></p></p><br><p><p></p></p><pre name="code" class="objc"><pre name="code" class="objc">UIWindow *window = [[uiwindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];levelviewcontroller = [[ Levelviewcontroller alloc] Init];window.rootviewcontroller = Levelviewcontroller;//similar to the following Sentence//[window AddSubView: levelviewcontroller.view]; [window makekeyandvisible];</pre></pre><p><p></p></p><span style="font-size:14px"><span style="font-size:14px">three ways to create a controller</span></span><span style="font-size:14px"><span style="font-size:14px">1. by Storyboardd</span></span><p><p><span style="font-size:14px">iOS will automatically perform the following actions</span></p></p><p><p><span style="font-size:14px">Instantiate a window</span></p></p><p><p></p></p><p><p><span style="font-size:14px">Load the primary storyboard and instantiate the initial Viewcontroller</span></p></p><p><p><span style="font-size:14px">Specifies that the view controller is the Window's root controller rootviewcontroller, which then causes the window to appear on the screen</span></p></p><p><p><span style="font-size:14px">Using the code description is probably:</span></p></p><pre name="code" class="objc"><pre name="code" class="objc">-(BOOL) application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{ Self.window = [[uiwindow alloc] initwithframe:[uiscreen mainscreen].bounds]; Uistoryboard *storyboard = [uistoryboard storyboardwithname:@ "Main" bundle:nil]; Qhviewcontroller * VC = [storyboard instantiateinitialviewcontroller]; Self.window.rootViewController = vc; [self.window makekeyandvisible]; Return YES;}</pre></pre><p><p><span style="font-size:14px">Note: the above three steps are iOS active, so the project to build Singleview will not see application:didfinishlaunchingwithoptions: there is nothing in the Method.</span></p></p><p><p><span style="font-size:14px">To use the other controllers inside the storyboard, you need to implement the Uistoryboard object</span></p></p><p><p><span style="font-size:14px">A. Obtaining Uistoryboard objects through the Storyboardwithname method of the Uistoryboard class</span></p></p><p><p><span style="font-size:14px">B. Using Instantiateinitialviewcontroller or Instantiateviewcontrollerwithidentifier: method to obtain the corresponding controller</span></p></p><p><p><span style="font-size:14px">The code is roughly as Follows:</span></p></p><pre name="code" class="objc"><pre name="code" class="objc">Uistoryboard *storyboard = self.storyboard; Specialviewcontroller *svc = [storyboard instantiateviewcontrollerwithidentifier:@ "specialviewcontroller"]; Configure The new view controller Here. [self presentviewcontroller:svc Animated:yes completion:nil];</pre></pre><span style="font-size:14px"><span style="font-size:14px">2. Using Xib</span></span><p><p><span style="font-size:14px">Xib is the predecessor of storyboard, the advantage of using storyboard is that you can set up the relationship of interface jump, it seems more Intuitive.</span></p></p><p><p><span style="font-size:14px">Create a class that inherits from Uiviewcontroller. Create a xib file that designates its file ' owner as a custom controller, and be sure to wire the View property to the view you want to display (because there may be multiple uiview in a xib file)</span></p></p><p><p></p></p><pre name="code" class="objc"><pre name="code" class="objc">Qhviewcontroller *controller=[[qhviewcontroller alloc]initwithnibname:@ "test" bundle:nil];</pre></pre><p><p></p></p><p><p><span style="font-size:14px">In fact, If the above sentence is written</span></p></p><p><p></p></p><pre name="code" class="objc"><pre name="code" class="objc">Qhviewcontroller *controller=[[qhviewcontroller alloc]init];</pre></pre><p><p></p></p><p><p><span style="font-size:14px">You can also load xib, except that you can't load a file named Text.xib.</span></p></p><p><p><span style="font-size:14px">Possible errors:</span></p></p><p><p><span style="font-size:14px">A was unable to load a nib named XXX: description Xib there is no view</span></p></p><p><p><span style="font-size:14px">b Loaded the XXX nib but the view outlet is not set: specify its file ' Sowner as a custom controller, and be sure to wire the View property to the view you want to display</span></p></p><span style="font-size:14px"><span style="font-size:14px">3. Using pure code</span></span><pre name="code" class="objc"><pre name="code" class="objc">-(void) applicationdidfinishlaunching: (uiapplication *) application { uiwindow *window = [[uiwindow alloc] Initwithframe:[[uiscreen mainscreen] bounds]; Levelviewcontroller = [[levelviewcontroller alloc] init]; Window.rootviewcontroller = levelviewcontroller; [window makekeyandvisible];}</pre></pre><p><p><span style="font-size:14px">The above sentence Levelviewcontroller = [[levelviewcontroller alloc] init]; When you create your own controller, you actually do the following:</span></p></p><p><p><span style="font-size:14px">A The default is to find The. Xib file that matches the controller, which means that if the controller class is qhfirstviewcontroller, then the matching. xib file is Qhfirstview.xib</span></p></p><p><p><span style="font-size:14px">b If it is not found, it looks for A. xib file with the same name as the controller</span></p></p><p><p><span style="font-size:14px">C If none of the above is found, a blank view is created to assign it to the Controller's view Property.</span></p></p><pre name="code" class="objc"><pre name="code" class="objc"></pre></pre><span style="font-size:14px"><span style="font-size:14px">How view is created in the controller</span></span><p><p><span style="font-size:14px">on</span> the <span style="font-size:14px">C-step of the polygon can be viewdidload by printing the Self.view in the method, and creating the view is done in the Loadview method. Viewcontroller calls this method when the view needs to be displayed and it is Nil. You can rewrite the Loadview method and find out that the program is not there to execute because the parent class will do the load view and call [super loadview]. You do not need to call [super loadview] with a plain code write layout, because the parent class of the method does not have to load the view through storyboard or xib, since there is no need to load the code from somewhere else if you want to customize it. </span></p></p><p><p><span style="font-size:14px"><span style="color:red">in fact, by</span> <span style="color:red">Storyboard</span> <span style="color:red">,</span> <span style="color:red">Xib</span> <span style="color:red">is also in</span> <span style="color:red">Loadview</span> <span style="color:red">method inside the Load. </span>the <span style="color:red">loadview</span> <span style="color:red">method does things as shown in</span></span></p></p><br><p><p></p></p><p><p><span style="font-size:14px">The official map is pretty good, showing what the Loadview method does, just loading the view through Xib without showing it, and the official document does not xib relevant content instead of storyboard, which is consistent with Apple's admiration for Storyboard.</span></p></p><p><p><br></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>iOS Learn how to create a 4_ controller and create a controller view</p></p></span>

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.