Swift用Delegate协议实现传值功能(一)

利用delegate实现两个Controller之间的传值


RootViewController中用个lable和一个按钮,点击按钮跳转到模态窗口。在模态窗口中有个TextField和一个按钮,输入文字点击关闭模态按钮后跳转到RootViewController,并改变其label为输入的值。

  • AppDelegate.swift

//  AppDelegate.swift
   import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate{

  var window: UIWindow?

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window?.backgroundColor = UIColor.darkGrayColor()
        self.window?.makeKeyAndVisible()
        let rootVc = RootViewController()
        self.window?.rootViewController = rootVc
        return true
    }
  • RootViewController.swift

import UIKit
//实现ModeViewControlDelegate协议
class RootViewController: UIViewController,MdoeViewControlDelegate{
    var btn:UIButton?
    var label:UILabel?
    //实现协议中的方法
    func changeLabel(newString:String)  {
        self.label!.text = newString
    }
    func btnOnClick(){
        print("Onclick")
        
        let modeView = ModeViewController()
        //设置modeView中的代理为RootViewController自身
        modeView.delegate=self
        //跳转到ModelView
        self.presentViewController(modeView,
                                   animated: true ,
                                   completion: {
                                    print("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 = "你好!"
        label!.textAlignment = .Center
        
        btn = UIButton()
        btn!.frame = CGRectMake(110, 80, 100, 20)
        btn!.backgroundColor = UIColor.greenColor()
        btn!.setTitle("下一视图", forState: .Normal)
        btn!.addTarget(self, action:#selector(RootViewController.btnOnClick), forControlEvents: .TouchUpInside)
        self.view.addSubview(label!)
        self.view.addSubview(btn!)
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
  • ModeViewControler.swift

import UIKit

class ModeViewController: UIViewController {
    var textF:UITextField?
    var delegate:MdoeViewControlDelegate?

    //点击事件
    func btnOnClick(){
        let str = textF?.text
        print(str)
        
        // 调用代理函数 改变label值
        self.delegate?.changeLabel(str!)
        //返回RootView
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor=UIColor.cyanColor()
        
        
        textF=UITextField()
        textF!.frame=CGRectMake(110,40,100,20)
        textF!.backgroundColor=UIColor.greenColor()
        textF!.borderStyle = .RoundedRect
        
        
        let btn=UIButton(frame:CGRectMake(110,80,100,20))
        btn.backgroundColor=UIColor.greenColor()
        btn.setTitle("返回上视图",forState:.Normal)
        //绑定事件
        btn.addTarget(self,action:#selector(ModeViewController.btnOnClick),forControlEvents: UIControlEvents.TouchUpInside)
        
        self.view.addSubview(btn)
        self.view.addSubview(textF!)
        
        
        // Do any additional setup after loading the view.
    }
}

  • Protocol.swift


import Foundation
//协议定义要实现的方法
protocol MdoeViewControlDelegate {
    func changeLabel(neString:String)
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,223评论 4 61
  • 夜,慢慢拉起了帷幕 青蛙上台,唱起了独角戏 哇,哇的…… 呱,呱的…… 咕,咕的…… 不晓得,它是在与谁对话 可我...
    就是那个零余者啊阅读 217评论 0 1
  • “你不理财,财不理你”这句标语时时充斥在理财市场上。不同于各种理财规划师的高谈阔论,作者汪标(标叔)认为:理财不能...
    菜小东西阅读 1,308评论 0 0
  • 走进reading town,做好我自己,影响更多人。亲子阅读记录--2月第4周 本周书单:《不一样的卡梅拉》,《...
    小时光倩阅读 298评论 0 0
  • 二探结界再生变故 几人商量了一会儿施行计划,火烨、珞珈、那谨便按随谓的方法结出了屏蔽结界将众人包裹起来,再...
    楚冬阅读 303评论 0 0