2014-06-05 96 views
4

我创建了2个视图,一个是和使用的协议和委托。对于第一个视图,不调用委托函数。委托没有在SWIFT中调用iOS

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

} 

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

} 
+0

你尝试过这一点,'委托.DurationSelected();!'? – holex

+0

是的。它与以下错误致命错误崩溃:无法展开Optional.None – PREMKUMAR

回答

10

对我来说,它看起来像你推动视图控制器,你还没有真正设置委托。如果你改变你的“下一步”功能,包括行

nextViewController.delegate = self 

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

0

您遵循的命名约定会混淆团队中的开发人员。实例应该已经

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

然后作为@Eagerod提到的,你将设置委托是

durationDel.delegate = self 
相关问题