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

查看:111
本文介绍了swift委托beetween两个视图控制器没有segue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代理协议从未调用



我的第一个控制器 - ViewController

  class ViewController:UIViewController,testProtocol {

@IBAction func btInit(sender:AnyObject){
println(Bt Init)

let storyBoard:UIStoryboard = UIStoryboard(name:Main,bundle:nil)
让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()
//处理可以重新创建的任何资源。
}

func testDelegate(){
println(在我的视图控制器委托中)
}
}



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

  protocol testProtocol {
func testDelegate()//这个函数的第一个控制器
}

class targetViewController:UIViewController {

@IBAction func BtTarget (sender:AnyObject){

println(bt target pressed)

委托?.testDelegate()
}

var delegate:testProtocol?

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

override func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning )
}

func testDelegate(){
println(在我的目标视图控制器委托中)
}
}

为什么在ViewController上没有调用testDelegate()?我做错了什么?谢谢。



我已经阅读了很多关于这个的文章,但所有的例子给出了segue过渡,我不想使用segue。

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

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

两个视图控制器中都有 testDelegate()方法,但只需要在第一个视图控制器中。然后你的第二个视图控制器可以在适当的时候调用 delegate?.testDelegate()



想要使委托属性变弱,所以我建议将 var delegate:testProtocol?更改为 weak var delegate:testProtocol?



我会阅读委托书。以下是一个相对简单的5步骤流程,可以帮助您:



5步授权:



对象A是对象B的委托,对象B将发出以下消息:


  1. 定义委托

  2. 给对象B一个可选的委托变量。

  3. 当某些有趣的事情(例如用户按下取消或完成按钮,或需要一段信息时)使对象B向其代理发送消息。

  4. 使对象A符合委托协议。

  5. 告诉对象B对象A现在是其委托(在prepareForSegue(sender)中)。
  6. / li>


My delegate protocol never called

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

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

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

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.

解决方案

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)

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.

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

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

Delegation in 5 Steps:

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

  1. Define a delegate protocol for object B.
  2. Give object B an optional delegate variable. This variable should be weak.
  3. Make object B send messages to its delegate when something interesting happens, such as the user pressing the Cancel or Done buttons, or when it needs a piece of information.
  4. Make object A conform to the delegate protocol. It should put the name of the protocol in its class line and implement the methods from the protocol.
  5. Tell object B that object A is now its delegate (in prepareForSegue(sender)).

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

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