没有segue的两个视图控制器之间的快速委托 [英] swift delegate beetween two view controller without segue

查看:17
本文介绍了没有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的两个视图控制器之间的快速委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆