"We all love Paul Hegarty." Stanford IOS8 public class personal note 23 multi-MVC mode demo implementation

Source: Internet
Author: User

In the last word, we did the automatic layout of the selection interface of the demo, and now we are going to connect multiple MVC operations. First we need files from other projects, so let's open another app. Click on the file below


Then drag the files we need into the new project directory:


Note Check the first line, or just make a reference, if you accidentally deleted the target directory, you will not find these files, so it is recommended to do replication, this will copy the files to our own project directory.

So what to do in storyboard, we just need to select the controller in the storyboard of the original project and then copy and paste it into the storyboard of the new project.

I noticed a problem when I copied and pasted the face of the controller when it was not there. It looks like a little bit of triggering is needed. We went to Faceview to do some minor changes and then back to save the good.


Look, it's coming out now. Now add these two MVC to the column controller, do not forget to move the left side of the small arrow to the column controller, the specific way we have already introduced, here is not verbose, directly see the result:


Let's run it on IP6 Plus, if it's a vertical screen you'll see:


Like any other iphone, you won't see a smiley face even if you swipe left and right, but if you cross-screen it will show:


This is the same as the ipad effect, we talked about IP6 Plus is special, although the screen is small but high definition, in the horizontal screen mode and the ipad is the same, about the automatic layout of things will be said later.

Before we proceed to the following work we need to modify the storyboard to fit the iphone, before we talk about embed in a navigation controller on master, now running on iphone6 you will see the initial interface become:

Click Back to return to the selection screen, if you add a title on the navigation bar, storyboard will appear warning, because our new navigation bar will block the previous label, the update frame is still good. Now we associate master with detail. We used segue, before the drag-and-drop button to the face of the controller and then choose Show Detail, three buttons are repeated action, three segue respectively named sad, Happy, meh (that is not happy and not sad).


Now you are running on the iphone can see the click Button page to jump, indicating that the segue effective, but the face of the villain has not changed, because we are not ready to segue, recall the previous talk.

Back in Psychologistviewcontroller's code, what we need to do in this code is to prepare for segue.

Import Uikitclass Psychologistviewcontroller:uiviewcontroller {    override func Prepareforsegue (segue: Uistoryboardsegue, Sender:anyobject?) {        if let HVC = Segue.destinationviewcontroller as? Happinessviewcontroller,let identifier = segue.identifier{            switch identifier{case            "sad": hvc.happiness = 0 Case            "Happy": hvc.happiness =            default:hvc.happiness = +                        }}}    

Please note that I am here if let (optional binding), which is the new version of Swift 1.2, avoids the case that there are many if let{} nesting. Looks like everything's fine? Then run a click on a button and you'll find the program crashes. The console tip found a null value when unpacking a selectable selection. Error location to UpdateUI here:


You will see that the Faceview is null value. Looking back at the previous code, we have just modified the value of the happiness to look at the Happiness's property viewer:

var happiness:int = 75 {//0 stands for sadness, 100 for Happy        didset{            happiness = max (happiness, 0)            println ("Happiness = \ (Happiness) ")            UpdateUI ()        }    }

You'll find that the UpdateUI method is called here, and the Faceview in this method is defined with Iboutlet!

This is what we said earlier that outlet was not initialized at the time of segue.

So how do we solve this problem? Fortunately, when we want to modify the value of the model will update the content of the page, if this time the page outlet no value, just ignore it. In a very simple way, we put a potentially empty value into the optional chain:

Func UpdateUI () {    faceview?. Setneedsdisplay ()    }

This means that if Faceview is empty, then none of the back will be executed. Just need a simple question mark and run again you'll find the effect we want. Now we add a navigation controller to the detail section, and the title is set according to the merit of the click button. Still embed in one.

Then add a line to the UpdateUI method:

Func UpdateUI () {    faceview?. Setneedsdisplay ()    title = "\ (Happiness)"    }

We have previously talked about the controller that has properties communicated to it by the navigation controller, and then the controller is responsible for drawing, running and looking, you will find that the title does not appear:

That's what we said earlier, when you add a navigation controller, all the segue point to the navigation controller, not the detail itself:


So how to modify it?

Import Uikitclass Psychologistviewcontroller:uiviewcontroller {    override func Prepareforsegue (segue: Uistoryboardsegue, Sender:anyobject?) {        var destination = Segue.destinationviewcontroller as? Uiviewcontroller        if let Navcon = destination as? uinavigationcontroller{        destination = Navcon.visibleviewcontroller        }        if let HVC = destination as? Happinessviewcontroller,let identifier = segue.identifier{            switch identifier{case            "sad": hvc.happiness = 0 Case            "Happy": hvc.happiness =            default:hvc.happiness = +                        }}}    

Here we make a judgment that we can apply this notation when we are not sure if a controller is in the navigation controller. If there is a navigation controller then remove the controller in the top controller is the demo Happinessviewcontroller, assign it to destination, if not the segue to save the target controller assigned to destination. Now it's ready to run:


Before the segue are all we achieved through the storyboard, now let's try to implement through the code, we add a new button named noting! The segue is then generated by dragging between pages. We select the yellow button above the master controller


Drag and drop to detail's navigation controller to choose Show Detail. Named nothing, then we open the Union view and drag the Nothing button into the controller code to generate a action,action code as follows:

@IBAction func Nothing (Sender:uibutton) {        performseguewithidentifier ("Nothing", Sender:nil)    }

You can see that we let sender for nil delegate Click on this button to do nothing, but it will still execute to the Prepareforsegue method, at this time we add a case in the Prepareforsegue method:

Case ' nothing ': hvc.happiness = 25

Run you'll find the Nothing button still works.



So why do we use segue in our code, and you might want to determine which segue to use based on some state after the button is clicked, which is a classic reason to use segue in your code.




"We all love Paul Hegarty." Stanford IOS8 public class personal note 23 multi-MVC mode demo implementation

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.