Swift Proxy and transmit value

Source: Internet
Author: User
Tags uikit

First View Controller:

Import UIKit

Follow the agreement

Class Viewcontroller:uiviewcontroller,secondvcdelegate

{

Override Func Viewdidload () {

Super.viewdidload ()

Create a button

Let button1 = UIButton (Frame:cgrectmake (120, 120, 50, 50))

Modify Background color

Button1.backgroundcolor = Uicolor.redcolor ()

Self.view.addSubview (button1)

click Method

Button1.addtarget (Self, Action: "Buttonaction:", ForControlEvents:UIControlEvents.TouchUpInside)

}

button click Method

Func buttonaction (Sender:uibutton) {

Jump to the next interface

Let SECONDVC = Secondviewcontroller ()

Property pass-through value

Secondvc.passvalue = "Xiu"

Specify Proxy

Secondvc.delegate = Self

Define Block

Secondvc.block = {(Tempcolor:uicolor)->void in

Self.view.backgroundColor = Tempcolor

}

Self.navigationcontroller?. Pushviewcontroller (SECONDVC, Animated:true)

}

Implementing Proxy methods

Func ChangeColor (Tempcolor:uicolor) {

Self.view.backgroundColor = Tempcolor

}

Override Func didreceivememorywarning () {

Super.didreceivememorywarning ()

}

}

/*************************************************************/

The six steps of the agent

1. Post-page Development agreement

2. Previous page Proxy Compliance protocol

3. Post-page Setup agent properties

4. The previous page specifies the agent self

5. Post-page Send Agent method command

6. Previous page proxy implementation Proxy method

Second View Controller:

Import UIKit

Protocol Secondvcdelegate {

Methods in the Protocol

Func ChangeColor (Tempcolor:uicolor)

}

Class Secondviewcontroller:uiviewcontroller {

Property

var passvalue:string?

Agent Properties

var delegate:secondvcdelegate?

Block Property

var block: ((uicolor)->void)?

Override Func Viewdidload () {

Super.viewdidload ()

Self.view.backgroundColor = Uicolor.cyancolor ()

Print (self.passvalue!)

Self.title = Self.passvalue

Return button

Let button2 = UIButton (Frame:cgrectmake (120, 120, 50, 50))

Button2.backgroundcolor = Uicolor.blackcolor ()

Button2.addtarget (Self, Action: "Button2action:", ForControlEvents:UIControlEvents.TouchUpInside)

Self.view.addSubview (Button2)

}

Button2 click Method

Func button2action (Sender:uibutton) {

Send Agent Method command

Self.delegate?. ChangeColor (Uicolor.redcolor ())

Call Block

self.block! (Uicolor.redcolor ())

Return

Self.navigationcontroller?. Popviewcontrolleranimated (True)

}

Override Func didreceivememorywarning () {

Super.didreceivememorywarning ()

Dispose of any resources the can be recreated.

}

}

Swift Proxy and transmit value

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.