代表未在SWIFT iOS中调用

时间:2014-06-05 16:29:53

标签: ios swift

我创建了2个视图,一个是使用的协议和代理。对于第一个视图,不调用Delegate函数。

My FirstView Controller:我在这里访问代理功能。

import UIKit

class NextViewController: UIViewController,DurationSelectDelegate {
    //var secondController: DurationDel?

    var secondController: DurationDel = DurationDel()

    @IBAction func Next(sender : AnyObject)
    {
        let nextViewController = DurationDel(nibName: "DurationDel", bundle: nil)
        self.navigationController.pushViewController(nextViewController, animated: true)
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        secondController.delegate=self
    }

    func DurationSelected() {
        println("SUCCESS")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

我的SecondView控制器:我在这里创建代表。

import UIKit

protocol DurationSelectDelegate {
    func DurationSelected()
}


class DurationDel: UIViewController {

    var delegate: DurationSelectDelegate?

    @IBAction func Previous(sender : AnyObject) {
        //let game = DurationSelectDelegate()
        delegate?.DurationSelected()
        self.navigationController.popViewControllerAnimated(true)
    }


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

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

2 个答案:

答案 0 :(得分:10)

对我而言,您似乎正在推动一个您尚未实际设置代理的视图控制器。如果你改变你的"下一步"功能,包括行

nextViewController.delegate = self

你应该看到代表团的工作。在这样做时,您也可以删除" secondController"的创建,因为它看起来像是多余的。

答案 1 :(得分:0)

您遵循的命名惯例会让您的团队中的开发人员感到困惑。该实例应该是

let durationDel = DurationDel(nibName: "DurationDel", bundle: nil)

然后正如@Eagerod所提到的,你要设置的代表是

durationDel.delegate = self