1. Function Introduction Using lable and a button in rootviewcontroller, click the button to jump to the modal window. There is a textfield and a button in the modal window. Enter the text and click the close modal button to jump to rootviewcontroller and change its label to the input value.
2. implementation idea: Define a member variable in modelviewcontroller. The member variable has a function that can change the label value. You can call this function in modelviewcontroller to change the label value in rootviewcontroller, because modelviewcontroller itself cannot directly change the member variables in rootviewcontroller, a proxy is defined in modelviewcontroller, which is implemented by rootviewcontroler.
3. Code
3.1protocol.swif
//// Protocol. swift // modelviewdemo /// created by Zhao Chao on 14-6-26. // copyright (c) 2014 Zhao Chao. all rights reserved. // import Foundation // protocol that defines the method protocol modeviewcontroldelegate {func changelabel (newstring: string )}
3.2appdelegate.swift
//// Appdelegate. swift // modelviewdemo /// created by Zhao Chao on 14-6-26. // copyright (c) 2014 Zhao Chao. all rights reserved. // import uikit @ uiapplicationmainclass appdelegate: uiresponder, uiapplicationdelegate {var window: uiwindow? Func application (Application: uiapplication, didfinishlaunchingwitexceptions launchoptions: nsdictionary ?) -> Bool {self. Window = uiwindow (frame: uiscreen. mainscreen (). bounds) // override point for customization after application launch. Self. Window !. Backgroundcolor = uicolor. whitecolor () self. Window !. Makekeyandvisible () var root = rootviewcontroller () self. Window !. Rootviewcontroller = root return true} func applicationwillresignactive (Application: uiapplication) {// sent when the application is about to move from active to inactive state. this can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. games shocould use this method to pause the game .} func applicationdidenterbackground (Application: uiapplication) {// use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // if your application supports background execution, this method is called instead of applicationwillterminate: when the user quits .} func applicationwillenterforeground (Application: uiapplication) {// called as part of the transition from the background to the inactive state; here you can undo changes of the changes made on entering the background .} func applicationdidbecomeactive (Application: uiapplication) {// restart any tasks that were paused (or not yet started) while the application was inactive. if the application was previusly in the background, optionally refresh the user interface .} func applicationwillterminate (Application: uiapplication) {// called when the application is about to terminate. save data if appropriate. see also applicationdidenterbackground :.}}
3.3rootviewcontroller.swift
//// Rootviewcontroller. swift // modelviewdemo /// created by Zhao Chao on 14-6-26. // copyright (c) 2014 Zhao Chao. all rights reserved. // import uikit // implement the modeviewcontroldelegate Protocol Class rootviewcontroller: uiviewcontroller, modeviewcontroldelegate {var BTN: uibutton? VaR label: uilabel? // Implement the method func changelabel (newstring: string) {self. Label !. TEXT = newstring} // button event func btnonclick () {println ("onclick") var modeview = modelviewcontroller () // set the proxy in modeview to rootviewcontroller's own modeview. delegate = self // jump to modelview self. presentviewcontroller (modeview, animated: True, completion: {println ("OK")} override func viewdidload () {super. viewdidload () self. view. backgroundcolor = uicolor. graycolor () label = uilabel () Label !. Frame = cgrectmake (110,40, 100,20) Label !. Backgroundcolor = uicolor. greencolor () Label !. TEXT = "Hello world! "Label !. Textalignment =. Center BTN = uibutton (frame: cgrectmake (110,80, 100,20) BTN !. Backgroundcolor = uicolor. greencolor () BTN !. Settitle ("enable mode", forstate:. Normal) BTN !. Addtarget (self, Action: "btnonclick", forcontrolevents: uicontrolevents. touchupinside) self. view. addsubview (BTN) self. view. addsubview (Label) // do any additional setup after loading the view .} override func didreceivememorywarning () {super. didreceivememorywarning () // dispose of any resources that can be recreated .} /* // # pragma mark-Navigation // In a storyboard-based application, you will ofte N want to do a little preparation before navigation override func prepareforsegue (segue: uistoryboardsegue ?, Sender: anyobject ?) {// Get the New View Controller Using [segue destinationviewcontroller]. // pass the selected object to the New View Controller .}*/}
3.4modelviewcontroller.swift
/// Modelviewcontroller. swift // modelviewdemo /// created by Zhao Chao on 14-6-26. // copyright (c) 2014 Zhao Chao. all rights reserved. // import uikitclass modelviewcontroller: uiviewcontroller {var textf: uitextfield? // Proxy member variable VAR delegate: modeviewcontroldelegate? // Click the event func btnonclick () {var STR = textf !. Text println (STR) // call the proxy function and change the label value self. Delegate !. Changelabel (STR) // returns rootview self. dismissmodalviewcontrolleranimated (true)} override func viewdidload () {super. viewdidload () view. backgroundcolor = uicolor. bluecolor () textf = uitextfield () textf !. Frame = cgrectmake (110,40, 100,20) textf !. Backgroundcolor = uicolor. greencolor () textf !. Borderstyle =. roundedrect var BTN = uibutton (frame: cgrectmake (110,80, 100,20) BTN. backgroundcolor = uicolor. greencolor () BTN. settitle ("Disable mode", forstate :. normal) // bind the event BTN. addtarget (self, Action: "btnonclick", forcontrolevents: uicontrolevents. touchupinside) self. view. addsubview (BTN) self. view. addsubview (textf) // do any additional setup after loading the view .}}