Swift uses protocol to implement proxy value-transfer function

Source: Internet
Author: User
Tags uikit

1. Brief introduction of functionRootviewcontroller with a lable and a button, click button to jump to the modal form. In the modal form, there is a textfield and a button, input text Click Close Modal button to jump to Rootviewcontroller, and change its label to enter the value.



2. Implementation ideasa member variable is defined in Modelviewcontroller, and the member variable has a function that can change the label value. By calling the function in Modelviewcontroller to change the value of the label in the Rootviewcontroller, Since Modelviewcontroller itself cannot directly change member variables in Rootviewcontroller, a proxy is defined in Modelviewcontroller that is implemented by Rootviewcontroler.
3. Code
3.1protocol.swif
  protocol.swift//  modelviewdemo////  Created by Carrie on 14-6-26.//  Copyright (c) 2014 Carrie. All rights reserved.//import foundation//protocol, defines the method to be implemented by the proxy protocol modeviewcontroldelegate{    func Changelabel ( newstring:string)}



3.2appdelegate.swift
appdelegate.swift//modelviewdemo////Created by Carrie on 14-6-26.//Copyright (c) 2014 Carrie.                                All rights Reserved.//import Uikit@uiapplicationmainclass Appdelegate:uiresponder, uiapplicationdelegate {    var Window:uiwindow?        Func application (application:uiapplication, Didfinishlaunchingwithoptions launchoptions:nsdictionary?), Bool { Self.window = UIWindow (Frame:UIScreen.mainScreen (). Bounds)//Override point for customization after Applicatio        N 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 are about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or WHEn the user quits the application and it begins the transition to the background state. Use the This method to the pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates.    Games should use this method to pause the game. } func Applicationdidenterbackground (application:uiapplication) {//Use this method to release shared resource s, save user data, invalidate timers, and store enough application state information to restore your application to its CU        Rrent 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's can undo many 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 is inactive.    If the application is previously in the background, optionally refresh the user interface. } func applicationwillterminate (application:uiapplication) {//Called when the application are about to Termina Te. Save data if appropriate.    See also Applicationdidenterbackground:. }}

3.3rootviewcontroller.swift


rootviewcontroller.swift//modelviewdemo////Created by Carrie on 14-6-26.//Copyright (c) 2014 Carrie. All rights Reserved.//import uikit//implements the Modeviewcontroldelegate protocol class Rootviewcontroller:uiviewcontroller,    Modeviewcontroldelegate {var Btn:uibutton?        var label:uilabel? Implement the method in the Protocol Func Changelabel (newstring:string) {self.label!. Text=newstring}//button event func Btnonclick () {println ("Onclick") var Modeview = MODELVIEWC Ontroller ()//Set the agent in Modeview to Rootviewcontroller itself modeview.delegate=self//jump to Modelview SELF.P            Resentviewcontroller (Modeview, animated:true, completion: {println ("OK") })} override Func Viewdidload () {super.viewdidload () Self.view.backgroundcolor=uicolor.gra Ycolor () 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 ("Open Modal", forstate:. Normal) btn!. AddTarget (self,action: "Btnonclick", ForControlEvents:UIControlEvents.TouchUpInside) Self.view.addSubview (BT    N) self.view.addSubview (label)//Do any additional setup after loading the view. } override func Didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of any resources t    Hat can be recreated. }/*//#pragma mark-navigation//In a storyboard-based application, you'll often want to do a little PR Eparation 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 Carrie on 14-6-26.//Copyright (c) 2014 Carrie.    All rights Reserved.//import Uikitclass Modelviewcontroller:uiviewcontroller {var textf:uitextfield?        Agent member variable var delegate:modeviewcontroldelegate? button click event Func Btnonclick () {var str=textf!. Text println (str)//Call proxy function, change the label value self.delegate!. Changelabel (str)//Return Rootview self.dismissmodalviewcontrolleranimated (True)} override Func Viewdidl Oad () {super.viewdidload () View.backgroundcolor=uicolor.bluecolor () Textf=ui TextField () 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.gre Encolor () btn.settitle ("Off Mode", Forstate:. Normal)//Bind event Btn.addtarget (sElf,action: "Btnonclick", ForControlEvents:UIControlEvents.TouchUpInside) Self.view.addSubview (BTN) s    Elf.view.addSubview (TEXTF)//Do any additional setup after loading the view. }}






















Swift uses protocol to implement proxy value-transfer function

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.