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