swift 用协议实现代理传值功能
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
swift 用協(xié)議實(shí)現(xiàn)代理傳值功能
原文??http://blog.csdn.net/whzhaochao/article/details/34903239
?
1.功能簡(jiǎn)介
RootViewController中用個(gè)lable和一個(gè)按鈕,點(diǎn)擊按鈕跳轉(zhuǎn)到模態(tài)窗口。在模態(tài)窗口中有個(gè)TextField和一個(gè)按鈕,輸入文字點(diǎn)擊關(guān)閉模態(tài)按鈕后跳轉(zhuǎn)到RootViewController,并改變其label為輸入的值。
2?.實(shí)現(xiàn)思路?
ModelViewController中定義一個(gè)成員變量,成員變量有個(gè)能改變label值的函數(shù),通過(guò)在ModelViewController中調(diào)用該函數(shù)從而改變RootViewController中l(wèi)abel的值,因?yàn)镸odelViewController自身不能直接改變RootViewController中的成員變量,所以在ModelViewController中定義一個(gè)代理,該代理由RootViewControler來(lái)實(shí)現(xiàn)?
?
3.代碼
3.1Protocol.swif
// // Protocol.swift // modelViewDemo // // Created by 趙超 on 14-6-26. // Copyright (c) 2014年 趙超. All rights reserved. //import Foundation //協(xié)議,定義代理要實(shí)現(xiàn)的方法 protocol ModeViewControlDelegate{func changeLabel(newString:String) }3.2AppDelegate.swift?
// // AppDelegate.swift // modelViewDemo // // Created by 趙超 on 14-6-26. // Copyright (c) 2014年 趙超. All rights reserved. //import UIKit@UIApplicationMain class 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 application launch.self.window!.backgroundColor = UIColor.whiteColor()self.window!.makeKeyAndVisible()var root=RootViewController()self.window!.rootViewController=rootreturn 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 should 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 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 was inactive. If the application was previously 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 趙超 on 14-6-26. // Copyright (c) 2014年 趙超. All rights reserved. //import UIKit// 實(shí)現(xiàn)ModeViewControlDelegate協(xié)議 class RootViewController: UIViewController,ModeViewControlDelegate {var btn:UIButton?var label:UILabel?//實(shí)現(xiàn)協(xié)議中的方法func changeLabel(newString:String){self.label!.text=newString}//按鈕事件func btnOnClick(){println("Onclick")var modeView = ModelViewController()//設(shè)置modeView中的代理為RootViewController自身modeView.delegate=self//跳轉(zhuǎn)到ModelViewself.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 = .Centerbtn=UIButton(frame:CGRectMake(110,80,100,20))btn!.backgroundColor=UIColor.greenColor()btn!.setTitle("打開(kāi)模態(tài)",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 often want to do a little preparation before navigationoverride 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 趙超 on 14-6-26. // Copyright (c) 2014年 趙超. All rights reserved. //import UIKitclass ModelViewController: UIViewController {var textF:UITextField?// 代理成員變量var delegate:ModeViewControlDelegate?//按鈕點(diǎn)擊事件func btnOnClick(){var str=textF!.textprintln(str)//調(diào)用代理函數(shù),改變Label值self.delegate!.changeLabel(str)//返回RootViewself.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 = .RoundedRectvar btn=UIButton(frame:CGRectMake(110,80,100,20))btn.backgroundColor=UIColor.greenColor()btn.setTitle("關(guān)閉模態(tài)",forState:.Normal)//綁定事件btn.addTarget(self,action:"btnOnClick",forControlEvents: UIControlEvents.TouchUpInside)self.view.addSubview(btn)self.view.addSubview(textF)// Do any additional setup after loading the view.}}轉(zhuǎn)載于:https://my.oschina.net/fadoudou/blog/699744
總結(jié)
以上是生活随笔為你收集整理的swift 用协议实现代理传值功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: IOS热更新-JSPatch实现原理+P
- 下一篇: android通过BitmapFacto