ios:协议、闭包、通知三种传值方式

<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"></span>
<span style="font-size:18px;">import UIKit

class ViewController: UIViewController,protocolVCDelegate {
    
    // 协议文本框
    @IBOutlet weak var protocolText: UITextField!
    
    // 闭包文本框
    @IBOutlet weak var closureText: UITextField!
    
    // 通知文本框
    @IBOutlet weak var notificationText: UITextField!
    
    // 协议传值
    @IBAction func protocolPassData(sender: UIButton) {
        
        let protocolVC = ProtocolVC()
        protocolVC.title = "协议传值界面"
        // 定义协议,实现协议方法
        protocolVC.delegate = self
        
        self.presentViewController(protocolVC, animated: true, completion: nil)
    }
    
    // 实现ProtocolVC中定义的协议方法
    func getTextValue(title: String) {
        self.protocolText.text = title
    }
    
    // 闭包传值
    @IBAction func closurePassData(sender: UIButton) {
        
        let closureVC = ClosureVC()
        closureVC.title = "闭包传值"
        
        // 实现闭包,通过回调传值
        closureVC.callBack = ({ (title:String) -> Void in
            self.closureText.text = title
        })
        
        self.presentViewController(closureVC, animated: true, completion: nil)
        
    }
    
    // 通知传值
    @IBAction func notificationPassData(sender: UIButton) {
        
        let notificationVC = NotificationVC()
        notificationVC.title = "通知传值"
        self.presentViewController(notificationVC, animated: true, completion: nil)
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func viewDidAppear(animated: Bool) {
        
        // 接收通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "notificationPD:", name: "notificationPassData", object: nil)
        
    }
    
    // 从通知函数中取值
    func notificationPD(info:NSNotification) {
        
        let text = info.userInfo!["text"] as? String
        if let str = text {
            self.notificationText.text = str
            NSNotificationCenter.defaultCenter().removeObserver(self, name: "notificationPassData", object: nil)
        }
        
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">协议方式:</span>
<span style="font-size:18px;">
// 定义协议
protocol protocolVCDelegate {
    func getTextValue(title:String)
}


class ProtocolVC: UIViewController {
    
    var textFiled:UITextField?
    
    var delegate:protocolVCDelegate?


    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.whiteColor()
        
        let screenWidth = self.view.frame.size.width
        
        textFiled = UITextField(frame: CGRectMake( 10, 40, screenWidth - 20, 35))
        textFiled?.borderStyle = UITextBorderStyle.RoundedRect
        textFiled?.placeholder = "输入..."
        textFiled?.becomeFirstResponder()
        
        let btn = UIButton(frame: CGRectMake( 10, 80, screenWidth - 20, 35))
        btn.setTitle("确定", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "save", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.view.addSubview(textFiled!)
        self.view.addSubview(btn)
        
    }
    
    // 确定
    func save() {
        self.delegate?.getTextValue(self.textFiled!.text!)
        self.dismissViewControllerAnimated(true) { () -> Void in
            
        }
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    


    /*
    // MARK: - Navigation


    // In a storyboard-based application, you will often 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.
    }
    */


}</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">
</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">闭包方式:</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">import UIKit


// 定义闭包
typealias ClosureCallBack = (title:String) -> Void


class ClosureVC: UIViewController {
    
    var textFiled:UITextField?
    
    // 定义闭包
    var callBack:ClosureCallBack?


    override func viewDidLoad() {
        super.viewDidLoad()


        self.view.backgroundColor = UIColor.whiteColor()
        
        let screenWidth = self.view.frame.size.width
        
        textFiled = UITextField(frame: CGRectMake( 10, 40, screenWidth - 20, 35))
        textFiled?.borderStyle = UITextBorderStyle.RoundedRect
        textFiled?.placeholder = "输入..."
        textFiled?.becomeFirstResponder()
        
        let btn = UIButton(frame: CGRectMake( 10, 80, screenWidth - 20, 35))
        btn.setTitle("确定", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "save", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.view.addSubview(textFiled!)
        self.view.addSubview(btn)


    }
    
    func save() {
        
        // 通过闭包传值
        self.callBack!(title: self.textFiled!.text!)
        self.dismissViewControllerAnimated(true, completion: nil)
        
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    


    /*
    // MARK: - Navigation


    // In a storyboard-based application, you will often 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.
    }
    */
}</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">
</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">通知</span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 18px;">方式</span><span style="font-family: Arial, Helvetica, sans-serif;">:</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">import UIKit


class NotificationVC: UIViewController {


    
    var textFiled:UITextField?
    
    override func viewDidLoad() {
        super.viewDidLoad()


        self.view.backgroundColor = UIColor.whiteColor()
        
        let screenWidth = self.view.frame.size.width
        
        textFiled = UITextField(frame: CGRectMake( 10, 40, screenWidth - 20, 35))
        textFiled?.borderStyle = UITextBorderStyle.RoundedRect
        textFiled?.placeholder = "输入..."
        textFiled?.becomeFirstResponder()
        
        let btn = UIButton(frame: CGRectMake( 10, 80, screenWidth - 20, 35))
        btn.setTitle("确定", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "save", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.view.addSubview(textFiled!)
        self.view.addSubview(btn)


    }
    
    func save() {
        
        let dataDic = NSMutableDictionary()
        dataDic.setValue(self.textFiled?.text, forKey: "text")
        
        // 注册通知
        let notification = NSNotification(name: "notificationPassData", object: nil, userInfo: dataDic as [NSObject:AnyObject] )
        // 发送通知
        NSNotificationCenter.defaultCenter().postNotification(notification)
        
        self.dismissViewControllerAnimated(true, completion: nil)
        
        
    }
    


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
</span></span></span>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值