DiscoverViewController

DiscoverViewController

我有两个视图控制器:“ DiscoverViewController”和“ LocationRequestModalViewController”。

用户第一次打开“ DiscoverViewController”时,我覆盖了“ LocationRequestModalViewController”,其中包含有关访问用户位置数据及其如何帮助他们的一些内容。

在“ LocationRequestModalViewController”上,有两个按钮:“不,谢谢”和“使用位置”。我需要将用户的响应发送回“ DiscoverViewController”

我进行了一些研究,发现代理/协议是最好的方法,因此我按照指南进行操作,但是我遇到了2个错误,无法弄清它们。

错误是:

在DiscoverViewController上

'DiscoverViewController' is not convertible to 'LocationRequestModalViewController'


在LocationRequestModalViewController上

'LocationRequestModalViewController' does not have a member name 'sendBackUserLocationDataChoice'


我已在以下文件中标记了错误发生的位置:

DiscoverViewController.swift

class DiscoverViewController: UIViewController, UITextFieldDelegate, CLLocationManagerDelegate, LocationRequestModalViewControllerDelegate {

    func showLocationRequestModal() {
        var storyboard = UIStoryboard(name: "Main", bundle: nil)
        var locationRequestVC: AnyObject! = storyboard.instantiateViewControllerWithIdentifier("locationRequestVC")
        self.presentingViewController?.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
        self.tabBarController?.presentViewController(locationRequestVC as UIViewController, animated: true, completion: nil)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        let vc = segue.destinationViewController as LocationRequestModalViewController
        vc.delegate = self //This is where error 1 happens
    }

    func sendBackUserLocationDataChoice(controller: LocationRequestModalViewController, useData: Bool) {
        var enableData = useData
        controller.navigationController?.popViewControllerAnimated(true)
    }

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


LocationRequestModalViewController

protocol LocationRequestModalViewControllerDelegate {
    func sendBackUserLocationDataChoice(controller:LocationRequestModalViewController,useData:Bool)
}

class LocationRequestModalViewController: UIViewController {

    var delegate:LocationRequestModalViewController? = nil

    @IBAction func dontUseLocationData(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    @IBAction func useLocationData(sender: AnyObject) {
        delegate?.sendBackUserLocationDataChoice(self, useData: true) // This is where error #2 happens
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //Modal appearance stuff here...
    }
}

最佳答案

答案就在您自己的问题上。这两个错误都说明了确切原因。

第1期

let vc      = segue.destinationViewController as LocationRequestModalViewController
vc.delegate = self //This is where error 1 happens


自我的类型为DiscoverViewController

但是您将委托声明为:

var delegate:LocationRequestModalViewController? = nil


您需要将其更改为:

var delegate:DiscoverViewController? = nil


第2期

相同的原因,LocationRequestModalViewController不能确认LocationRequestModalViewControllerDelegate,请更改委托声明。

关于ios - Swift中的协议(protocol)和代表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27953429/

10-11 12:11