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

    1. <legend id='kd5rC'><style id='kd5rC'><dir id='kd5rC'><q id='kd5rC'></q></dir></style></legend>
    2. <tfoot id='kd5rC'></tfoot>
      <i id='kd5rC'><tr id='kd5rC'><dt id='kd5rC'><q id='kd5rC'><span id='kd5rC'><b id='kd5rC'><form id='kd5rC'><ins id='kd5rC'></ins><ul id='kd5rC'></ul><sub id='kd5rC'></sub></form><legend id='kd5rC'></legend><bdo id='kd5rC'><pre id='kd5rC'><center id='kd5rC'></center></pre></bdo></b><th id='kd5rC'></th></span></q></dt></tr></i><div id='kd5rC'><tfoot id='kd5rC'></tfoot><dl id='kd5rC'><fieldset id='kd5rC'></fieldset></dl></div>
    3. <small id='kd5rC'></small><noframes id='kd5rC'>

      没有segue的两个视图控制器之间的快速委托

      时间:2023-09-12
      <legend id='uUXzo'><style id='uUXzo'><dir id='uUXzo'><q id='uUXzo'></q></dir></style></legend>
        <bdo id='uUXzo'></bdo><ul id='uUXzo'></ul>

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

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

            • <tfoot id='uUXzo'></tfoot>
                <tbody id='uUXzo'></tbody>
              • 本文介绍了没有segue的两个视图控制器之间的快速委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我的第一个控制器 - ViewController

                My first controller - ViewController

                class ViewController: UIViewController,testProtocol {
                
                    @IBAction func btInit(sender: AnyObject) {
                        println("Bt Init")
                
                        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                        let initViewController: UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewTarget") as targetViewController
                        self.presentViewController(initViewController,animated: false, nil)
                
                    }
                
                    var targetController = targetViewController();
                
                    override func viewDidLoad() {
                        super.viewDidLoad()
                        self.targetController.delegate = self
                    }
                
                    override func didReceiveMemoryWarning() {
                        super.didReceiveMemoryWarning()
                        // Dispose of any resources that can be recreated.
                    }
                
                    func testDelegate(){
                        println(" in my view controller delegate ")
                    }
                }
                

                在我的第二个视图控制器 - targetViewController

                In my second view controller - targetViewController

                protocol testProtocol {
                    func testDelegate() // this function the first controllers
                }
                
                class targetViewController: UIViewController {
                
                    @IBAction func BtTarget(sender: AnyObject) {
                
                        println("bt target pressed")
                
                        delegate?.testDelegate()
                    }
                
                    var delegate : testProtocol?
                
                    override func viewDidLoad() {
                        super.viewDidLoad()
                    }
                
                    override func didReceiveMemoryWarning() {
                        super.didReceiveMemoryWarning()
                    }
                
                    func testDelegate(){
                        println(" in my target view controller delegate ")
                    }
                }
                

                为什么从来没有在 ViewController 上调用 testDelegate()?我究竟做错了什么?谢谢.

                Why is testDelegate() never called on ViewController? What am I doing wrong? Thanks.

                我已经阅读了很多关于这个的帖子,但是所有的例子都是用 segue 转换给出的,我不想使用 segue.

                I have read a lot of posts about this, but all of the examples are given with segue transition, and I don't want use a segue.

                推荐答案

                通常你在 prepareForSegue: 中设置一个新的视图控制器的委托属性.你说你没有使用 segue,所以你需要实例化第二个视图控制器并以某种方式呈现它.你可以这样做:

                Typically you set a new view controller's delegate property in prepareForSegue:. You said you're not using a segue, so you'll need to instantiate the second view controller and present it somehow. You can do this by doing something like:

                let storyboard = UIStoryboard(name: "AStoryboardName", bundle: nil)
                let secondVC = storyboard.instantiateViewControllerWithIdentifier(anIdentifier) as! targetViewController
                secondVC.delegate = self
                presentViewController(secondVC, animated: true, completion: nil)
                

                您在 both 视图控制器中有一个 testDelegate() 方法,但您只希望在第一个视图控制器中使用它.然后你的第二个视图控制器可以在适当的时候调用 delegate?.testDelegate().

                You have a testDelegate() method in both view controllers, but you only want it in the first view controller. Then your second view controller can call delegate?.testDelegate() at the appropriate time.

                最后,您通常想让委托属性变弱,所以我建议将 var delegate : testProtocol? 更改为 weak var delegate: testProtocol?

                Finally, you typically want to make delegate properties weak, so I would recommend changing var delegate : testProtocol? to weak var delegate: testProtocol?

                我会阅读代表团.这是一个相对简单的 5 步委派过程,可能会对您有所帮助:

                I would read up on delegation. Here is a relatively simple 5 step process to delegation that may help you:

                五步委派:

                对象 A 是对象 B 的委托,对象 B 会发出消息:

                object A is the delegate for object B, and object B will send out the messages:

                1. 为对象 B 定义一个委托协议.
                2. 给对象 B 一个可选的委托变量.这个变量应该很弱.
                3. 当有趣的事情发生时,让对象 B 向其代理发送消息,例如用户按下取消或完成按钮,或者当它需要一条信息时.
                4. 使对象 A 符合委托协议.它应该将协议的名称放在其类行中并实现协议中的方法.
                5. 告诉对象 B 对象 A 现在是它的委托(在 prepareForSegue(sender) 中).

                这篇关于没有segue的两个视图控制器之间的快速委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:iOS Multipeer 连接框架invitationHandler 似乎不接受? 下一篇:为 UIImagePicker 设置委托返回错误

                相关文章

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

              • <small id='MP9ED'></small><noframes id='MP9ED'>