iOS learning the Stanford University iOS Development Course Note (Lesson six)

Source: Internet
Author: User

Reprint please indicate the source

http://blog.csdn.net/pony_maggie/article/details/28398697

Author: Pony


This session focuses on how multiple MVC works together. So far, all the courses have only involved a single view. From this class onwards, you will be given examples of multi-view applications.



Multi-view development here with Navigationcontroller for example. This is also more commonly used. As shown, a navigation Controller (MVC) points to a rootviewcontroller (another MVC), which is the first MVC to be shown.


in the navigation controller, connecting each MVC is through a thing called segue. . In storyboard, you only need to hold down the control key and drag a control from one controller (such as a button) onto another controller. This creates a segue, and then we can set the identifier for the segue so that it can be controlled in the code. For example, the following sample code:

-(void) setandshowdiagnosis: (int) diagnosis{    self.diagnosis = diagnosis;    [Self performseguewithidentifier:@ "showdiagnosis" sender:self];}



We add a navigation controller through the menu bar. It's easy to embed it in our rootviewcontroller.


Show us what the app looks like with a navigation feature. The middle part is its view (UIView), the upper middle part is the navigation bar, the middle text is the title, the left is Backbutton, it is a uibarbuttonitems control.


In practice, there are very few opportunities to use the popviewcontrolleranimated function. In most cases we will use the back button mentioned above to return to the top level view.


There are many kinds of segue mentioned here, although this is only used to push, the other is necessary to introduce. Please read the English instructions carefully, it is not difficult to understand.


Sometimes we will need to trigger the segue in the code (rather than the drag mentioned above). The method is through the Performseguewithidentifier function. Under what circumstances will this need be? For example, we have a segue that can only be established under certain conditions. See the example code above.



There is also a very important and commonly used method for segue, The prepareforsegue function. We often pass the data in this function to the controller that is going to switch over through the navigation. This means that this function occurs at some point before the new view is loaded . This is also available in the sample code:

Pass data-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{    if ([Segue.identifier isequaltostring:@ "Showdiagnosis"])    {        [Segue.destinationviewcontroller setHappiness:self.diagnosis];    }    else if ([Segue.identifier isequaltostring:@ "Celebrity"])    {        [Segue.destinationviewcontroller sethappiness : [+];    }    else if ([Segue.identifier isequaltostring:@ "serious"])    {        [Segue.destinationviewcontroller sethappiness :];    }    else if ([Segue.identifier isequaltostring:@ "TV kook"])    {        [Segue.destinationviewcontroller sethappiness : [];    }        }


The following is the code demo time.

We will create a new project called psychologist (psychologist), which is a very simple demo, based on the question we answered, set the degree of "happiness", and then change the smile level of the smiley face (also remember the happiness example of the lesson). So here are some of the code for the last lesson.


I have analyzed some of the relevant core code above, and there is not much to say here. But one more thing to start with in detail is how to reuse the code from the last lesson. The operation is not very troublesome, but it is important.

First we found the following four files from the original Happiness project folder and dragged them to the psychologist project, as shown in:


Then we add a new Viewcontroller in the storyboard, and give him a view, and then set the controller corresponding to the class Happinessviewcontroller, the view corresponding class is Faceview, As shown in the following:


Also, to connect the view to the controller, as shown in:

The results of the final run are as follows:


Code:

Https://github.com/pony-maggie/Psychologist

Or

http://download.csdn.net/detail/pony_maggie/7442627


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.