"We all love Paul Hegarty" Stanford IOS8 public class personal note deledgation agent

Source: Internet
Author: User

In the previous session, we introduced the knowledge of extensions and protocols, which we introduced a very important concept delegation (proxy), the agent is a very important application of the protocol. Let's review the knowledge of the agent, which is the key to the controller and the attempt to communicate.


So how did the agency work?


1. You need to create an agent protocol that describes what this is trying to do for others.

2. Create a property in your view, called a proxy or sometimes called a data source, which is the type of proxy protocol you create.

3. Then you use this property to handle all the agents, the proxy attribute will go to request the data it needs, remember that the attempt itself cannot own the data. As long as the object that follows this protocol can set a value to the property in this protocol, our view will know how to do it after the value is given.

4. The controller first says that it implements the Protocol itself, and then it acts as a proxy object.

5. The controller implements all the proxy methods so that it follows the protocol

Now the controller has a connection to the view, although it does not know what the class that controls it is, what the data is, the only thing it knows is that the controller implements the Protocol method in the view.

Let's go back to our little face demo to see how we can do this.


Let the agent in Faceview get its data, this data is its smiliness, and Happinessviewcontroller will become its controller.

Add the following code to the Faceview:

Protocol Faceviewdatasource {  func smilinessforfaceview (sender:faceview)-Double?}

Probably our naming should be faceviewdelegate, but the function of this protocol is mainly to return the value of smiliness, this proxy is called DataSource, so we named it so. The parameter type passed in is Faceview, which will pass itself in, and in iOS if an object has a delegate or datasource, they will pass in themselves.

The second step is that in our faceview we need to have a public variable:

var datasource:faceviewdatasource?
You can see our protocol as a data type here, so if we want to make a protocol our proxy, we just need to set the protocol as the value of the class variable, using the selectable is because there is no controller to follow our proxy protocol, then our small face will be a straight line will not change, The protocol can be nil, although usually we don't want it to be nil.

We want it to be of a weak type:

Weak var datasource:faceviewdatasource?

This involves memory management knowledge, in fact, the memory is automatically managed by the operating system, so there is no need to worry about. However, it is important to note that if the controller takes itself as a proxy, then it points the pointer to itself, unfortunately if our controller already has a faceview pointer on the view layer, because it has a outlet connected to Faceview, so that two objects point to each other. In this case they will always reference each other in memory, such a loop in memory is very scary, because the controller and view can not free memory, they will stay in memory forever, so the use of the weak keyword means that no matter what it points to the object, it should not be left in memory. Usually we do not use weak, but in iboutlet weak is heavily used, the use of proxies is another application to the weak place. So when we have an agent we always need a variable of type weak.

You will find that the above sentence error, prompting US weak can only be used in the class, although our agent is a protocol, but in the previous words, you can use the following wording to set it to only be class to follow:

Protocol Faceviewdatasource:class {  func smilinessforfaceview (sender:faceview)-Double?}

Now this proxy will not be implemented by enumerations and structs.

Before the value of Smiliness is written dead, now we want the value of smiliness to be provided by the agent, making the following modifications:

        Let smiliness = DataSource?. Smilinessforfaceview (self)

You will see the following sentence error:

Let Smilepath = Bezierpathforsmile (smiliness)

This is because our smiliness is an optional value, and the parameters of bezierpathforsmile must not be empty, so what do we do, we have to come into contact with a cool feature of Swift, that is?? operator.

Let smiliness = DataSource?. Smilinessforfaceview (self)?? 0.0

?? The operator means that if the left expression is nil then it is used?? The value on the right is returned, if the left value is not nil to return to the left value, now if the agent passed the value is empty, then smiliness will get 0.0 of the value, the small face of the mouth is a straight line.

Now let us come to the other side of the implementation of the agent, namely Happinessviewcontroller.

First let it comply with the agreement:

Class happinessviewcontroller:uiviewcontroller,faceviewdatasource{

Once you write this you will receive an error message indicating that you did not implement the proxy method.

Import Uikitclass happinessviewcontroller:uiviewcontroller,faceviewdatasource{    var happiness:int = 50 {//0 stands for sadness, 100 stands        for Happy didset{            happiness = max (min (Happiness, h), 0)            println ("happiness = \ (Happiness)")            UpdateUI ()        }    }    func UpdateUI () {        }    func Smilinessforfaceview (sender:faceview), Double? {        < #code #>    }}
Only the first few letters that need to enter the proxy method are automatically associated. The proxy method does not know what happiness is doing, because it is the model layer, the task of the controller is to parse the model for the view, you will also parse the view for the model, which will be mentioned when gesture recognition.

Then the contents of the proxy method are:

Func Smilinessforfaceview (Sender:faceview), Double? {        return Double (happiness-50)/50    }

In this way, we turn the data in the model into the format needed for the view. The final step now is for the controller to use itself as the Faceview data source.

Now back in Faceview, there's a trick: pressing the Command+shift+o key and typing in the keywords will quickly find the files in your project, which is a great way to get your project going.


Note Now that our storyboard shows the Faceview, remember the @ibdesignable we set up before? We need to drag a faceview on the controller to show

@IBOutlet weak var faceview:faceview!

Then add a property detector to it that will be called when iOS launches the app and loads the storyboard, which is a good time

@IBOutlet weak var faceview:faceview! {        didset{        faceview.datasource = self        }    }

The other thing to do is to let Faceview redraw itself every time our model changes. We need to complete UpdateUI this method:

Func UpdateUI () {    faceview.setneedsdisplay ()    }

The meaning here is to call my drawrect function and redraw me. If you do not do this step then modify the value of the happiness the face of the little man will not change. Let's change the value of happiness to 75 to run a look at the effect:


A very cute smiley face.


"We all love Paul Hegarty" Stanford IOS8 public class personal note deledgation agent

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.