如何在UIViewControllers与协议/代理

时间:2016-08-02 13:10:09

标签: ios swift delegates protocols pass-data

在下面的代码中,我有一个ViewController(“SenderViewController”),当点击一个按钮时,它会将消息传递给主ViewController。我不完全理解的是主messageData()中的ViewController方法如何知道何时收听消息。

有人可以解释我在主messageData()中触发ViewController方法的原因吗?

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!)  
        }  
    }  
}  

主ViewController:

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)"  
    }  
} 

非常感谢!

5 个答案:

答案 0 :(得分:3)

对象并不完全听取方法调用。他们坐在那里,等待援引。

该行

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

来自您的SenderViewController是一个函数调用。 (术语方法和函数几乎可以互换,尽管method通常用于对象的函数。)它调用ViewController中的函数messageData。

答案 1 :(得分:3)

SenderViewController展示MainViewController时,您将委托设置为self。所以无论何时在SenderViewController中调用委托方法

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

MainViewController的以下方法将充当回调

func messageData(data: AnyObject) {  
        self.showData.text = "\(data)"  
    } 

答案 2 :(得分:3)

SenderViewController

点按按钮时,您会调用sendData方法。在此方法中,您要求delegate调用其messageData方法。委托声明为SenderViewControllerDelegate类型的属性,因此您可以这样做(请参阅此协议防御)。

ViewController(第一个视图控制器)中:

在您打开第二个视图控制器之前,在方法goToView中,您将delegate的{​​{1}}的属性设置为自己',以确切的{{1}实例因为你声明它通过实现方法SenderViewController来确认协议ViewController。因此,SenderViewControllerDelegate现在在messageData中保存为ViewController属性,可用于调用delegate

答案 3 :(得分:2)

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:func="http://www.functx.com">
  <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
  <xsl:strip-space elements="*"/>

    <xsl:template match="/shiporder[1]">
        <Customer>
            <xsl:apply-templates select="item"/>
        </Customer>
  </xsl:template>

  <xsl:template match="/shiporder[1]/item">
      <adress>
         <xsl:value-of select="text()"/>
      </adress>
  </xsl:template>
</xsl:stylesheet>

答案 4 :(得分:0)

@IBAction func sendData(sender: AnyObject) {  

    if inputMessage.text != ""{  
        self.delegate?.messageData(inputMessage.text!)  
        self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)  

    }else{
       //handle here
}

注意:如果需要将多个数据传递给mainViewController,请使用字典来传递它们。即

SenderViewController:

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

    @IBAction func sendData(sender: AnyObject) {  

        let myDict = [ "name": "Name", "age": 21, "email": "test@gmail.com"] as! [String : Any]

        self.delegate?.messageData(myDict) 
        self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)  


  }  
}  

主ViewController

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: [String : Any]) {  
        print(data["name"])  
        print(data["age"])  
        print(data["email"])  

    }  
}