如何使用协议/委托在 UIViewControllers 之间传递数据

How to pass data between UIViewControllers with protocols/delegates(如何使用协议/委托在 UIViewControllers 之间传递数据)
本文介绍了如何使用协议/委托在 UIViewControllers 之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我有一个 ViewController("SenderViewController"),当点击按钮时,它会向主 ViewController 传递一条消息.我不完全理解的是主 ViewController 中的 messageData() 方法如何知道何时监听消息.

In the code below I have a ViewController("SenderViewController"), which passes a message to the main ViewController when a button is tapped. What I don't fully understand is how does messageData() method in the main ViewController know when to listen for the message.

谁能解释一下是什么触发了主 ViewController 中的 messageData() 方法?

Can someone please explain me what is triggering the messageData() method in the main ViewController?

SenderViewController:

SenderViewController:

import UIKit  
protocol SenderViewControllerDelegate {  
    func messageData(data: AnyObject)  
}  
class SenderViewController: UIViewController {  
    @IBOutlet weak var inputMessage: UITextField!  
     var delegate: SenderViewControllerDelegate?  

    @IBAction func sendData(sender: AnyObject) {  
        /  
        if inputMessage.text != ""{  
            self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)  
            self.delegate?.messageData(inputMessage.text!)  
        }  
    }  
}  

主视图控制器:

import UIKit  
class ViewController: UIViewController, SenderViewControllerDelegate{  
    @IBOutlet weak var showData: UILabel!  

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

    @IBAction func goToView(sender: AnyObject) {  
        let pvc = storyboard?.instantiateViewControllerWithIdentifier("senderViewController") as! SenderViewController  
        pvc.delegate = self  
        self.presentViewController(pvc, animated:true, completion:nil)  
    }  

   // What triggers this method, how it know when to listen? 
    func messageData(data: AnyObject) {  
        self.showData.text = "(data)"  
    }  
} 

非常感谢!

推荐答案

对象并不完全监听方法调用.他们坐在那里,等待被调用.

Objects don't exactly listen for method calls. They sit there, waiting to invoked.

线

self.delegate?.messageData(inputMessage.text!)

从您的 SenderViewController 是一个函数调用.(虽然 method 通常用于对象的功能,但术语方法和函数几乎可以互换.)它调用 ViewController 中的函数 messageData.

From your SenderViewController is a function call. (The term method and function are pretty much interchangeable, although the method is usually used for the functions of objects.) It invokes the function messageData in ViewController.

这篇关于如何使用协议/委托在 UIViewControllers 之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)