代表未在SWIFT iOS中调用

PRE*_*MAR 4 ios swift

我创建了2个视图,一个是使用协议和委托.对于第一个视图,不调用Delegate函数.

我的FirstView控制器:我在这里访问代理功能.

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.
    }

}
Run Code Online (Sandbox Code Playgroud)

我的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.
    }

}
Run Code Online (Sandbox Code Playgroud)

小智 10

对我来说,看起来你正在推动一个你没有实际设置委托的视图控制器.如果更改"下一步"功能,则包括该行

nextViewController.delegate = self
Run Code Online (Sandbox Code Playgroud)

你应该看到代表团的工作.在执行此操作时,您还可以删除"secondController"的创建,因为它看起来是多余的.