A summary of the Swift version of Miss no Sister (iii)
Chapter Content
- Use of common value-transfer methods
- Classic Error
use of common value-transfer methodsOne: Closures (closures)For example, in OC we need to define a block in the view, and then in the corresponding controller using block to save the corresponding weights fast, and then return to execute here, that in OC is similar, but the name of a different syntax there is a little difference between the first in the view to declare a closure alias, According to the parameters we need to pass
/// Closure Declaration Typealias icocosclosure = (string
Then declare a closure property and implement the initialization method
var icocosblock:icocosclosure? // assign a function pointer to the Myclosure closure, which covers references to local variables in the Somefunctionthattakesaclosure function, etc. Func initwithclosure (closure:icocosclosure?) { = closure }
Finally, in the method of shaking execution, it can be judged and executed.
// empty sentence if (Icocosblock! = nil) {// closed Baoyin call somefunctionthattakesaclosure function: callback. icocosblock! ( string " name " )
This time the controller inside is very simple, only need to pass in the corresponding code to save in the closure
Textfield.initwithclosure {(stringin if (Self.textfield?. Text.isempty! = nil) {return; } Self.tagButtons.removeLast () }
Second: AgentFirst, define an agent protocol, and the corresponding proxy method
// protocol that defines the method to be implemented by the agent protocol icocosviewcontroldelegate{ func Changelabel (newstring:string)}
Define a Delegate object
delegate: Icocosviewcontroldelegate?
Determine whether to abide by this proxy protocol and follow the proxy method
if (delegate) { delegate?. Changelabel ("icocos" )}
Implement the following agent in the controller or view of the receiving agent
set up proxy vcs. delegate = self
Compliance Agreement Icocosviewcontroldelegate
Comply with protocol icocosviewcontroldelegate in the corresponding controller, implement proxy method
Implement Proxy Method func Changelabel (newstring:string) { implements a gift method of proxy Here's the idea of passing the arguments}
Three: Notice, almost the same as OCAdd a notification and implement the synchronization method
- Nsnotificationcenter.defaultcenter (). Addobserver (Self, selector: "Notification", Name: "Icocos", object:self)
func Notification () { print ("")
Release notifications on the corresponding controller
- Nsnotificationcenter.defaultcenter (). Postnotificationname ("Icocos", Object:self, Userinfo:nil)
Four: Segue implement controller Jump
1 override func prepareforsegue ( Segue:uistoryboardsegue, Sender:anyobject? 3 if segue.identifier = = showdetail { 4 destinationController.navigationItem.title = " The dish to be shaken 5 Destinationcontroller.tempcaipin = Self.showarray // pass value 6 7 }
Five: Nsuserdefaults (but this uses more is data persistence, here strictly cannot calculate the value of the pass)
1 func cachesetstring (key:string,value:string) {2var userInfo =nsuserdefaults ()3 Userinfo.setvalue (value, Forkey:key)4 }5 6Func cachegetstring (key:string)string{7var userInfo =nsuserdefaults ()8var tmpsign =Userinfo.stringforkey (Key)9 returntmpsign!Ten}
Classic ErrorOne: Optional type error
- Tipbtn.settitle ("Add Emoticons: \ (Self.textfield?. Text) ", ForState:UIControlState.Normal)
Solutions
- Tipbtn.settitle ("Add Emoticons: \ (self.textfield!. Text) ", ForState:UIControlState.Normal)
Two: Optional chainPrompt for insertion
After inserting the child?? Prompt to delete
This time just use the display to
Copyright NOTICE: Welcome Reprint, please paste the source address: http://www.cnblogs.com/iCocos/(iOS DreamWorks)
For more highlights, please follow GitHub:https://github.com/al1020119?tab=repositories
iOS Development--full project Combat Swift & Miss Miss Swift Edition summary (iii)