<bdo id='nACZm'></bdo><ul id='nACZm'></ul>
  • <tfoot id='nACZm'></tfoot>
    <i id='nACZm'><tr id='nACZm'><dt id='nACZm'><q id='nACZm'><span id='nACZm'><b id='nACZm'><form id='nACZm'><ins id='nACZm'></ins><ul id='nACZm'></ul><sub id='nACZm'></sub></form><legend id='nACZm'></legend><bdo id='nACZm'><pre id='nACZm'><center id='nACZm'></center></pre></bdo></b><th id='nACZm'></th></span></q></dt></tr></i><div id='nACZm'><tfoot id='nACZm'></tfoot><dl id='nACZm'><fieldset id='nACZm'></fieldset></dl></div>
  • <legend id='nACZm'><style id='nACZm'><dir id='nACZm'><q id='nACZm'></q></dir></style></legend>

    1. <small id='nACZm'></small><noframes id='nACZm'>

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

        How to pass data between UIViewControllers with protocols/delegates(如何使用协议/委托在 UIViewControllers 之间传递数据)

            <bdo id='AuQe1'></bdo><ul id='AuQe1'></ul>

          • <tfoot id='AuQe1'></tfoot>
              <tbody id='AuQe1'></tbody>
          • <legend id='AuQe1'><style id='AuQe1'><dir id='AuQe1'><q id='AuQe1'></q></dir></style></legend>

                <i id='AuQe1'><tr id='AuQe1'><dt id='AuQe1'><q id='AuQe1'><span id='AuQe1'><b id='AuQe1'><form id='AuQe1'><ins id='AuQe1'></ins><ul id='AuQe1'></ul><sub id='AuQe1'></sub></form><legend id='AuQe1'></legend><bdo id='AuQe1'><pre id='AuQe1'><center id='AuQe1'></center></pre></bdo></b><th id='AuQe1'></th></span></q></dt></tr></i><div id='AuQe1'><tfoot id='AuQe1'></tfoot><dl id='AuQe1'><fieldset id='AuQe1'></fieldset></dl></div>

                  <small id='AuQe1'></small><noframes id='AuQe1'>

                • 本文介绍了如何使用协议/委托在 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 之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  iOS AutoLayout - get frame size width(iOS AutoLayout - 获取帧大小宽度)
                  Auto layout constraints issue on iOS7 in UITableViewCell(UITableViewCell中iOS7上的自动布局约束问题)
                  How to resize superview to fit all subviews with autolayout?(如何调整超级视图的大小以适应所有具有自动布局的子视图?)
                  Springs in Auto Layout: Distribute views evenly, with constraints, in Xcode 5(自动布局中的弹簧:在 Xcode 5 中使用约束均匀分布视图)
                  reloadData() of UITableView with Dynamic cell heights causes jumpy scrolling(具有动态单元格高度的 UITableView 的 reloadData() 导致跳跃滚动)
                  What is NSLayoutConstraint quot;UIView-Encapsulated-Layout-Heightquot; and how should I go about forcing it to recalculate cleanly?(什么是 NSLayoutConstraint“UIView-Encapsulated-Layout-Height?我应该如何强制它干净地重新计算?) - IT屋-程序员
                • <i id='Nbrly'><tr id='Nbrly'><dt id='Nbrly'><q id='Nbrly'><span id='Nbrly'><b id='Nbrly'><form id='Nbrly'><ins id='Nbrly'></ins><ul id='Nbrly'></ul><sub id='Nbrly'></sub></form><legend id='Nbrly'></legend><bdo id='Nbrly'><pre id='Nbrly'><center id='Nbrly'></center></pre></bdo></b><th id='Nbrly'></th></span></q></dt></tr></i><div id='Nbrly'><tfoot id='Nbrly'></tfoot><dl id='Nbrly'><fieldset id='Nbrly'></fieldset></dl></div>

                  <tfoot id='Nbrly'></tfoot>

                  <small id='Nbrly'></small><noframes id='Nbrly'>

                      • <bdo id='Nbrly'></bdo><ul id='Nbrly'></ul>

                              <tbody id='Nbrly'></tbody>
                            <legend id='Nbrly'><style id='Nbrly'><dir id='Nbrly'><q id='Nbrly'></q></dir></style></legend>