簡體   English   中英

Swift中的協議和代表

[英]Protocols and Delegates in Swift

我有兩個視圖控制器:“ 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確認,更改委托聲明。

你定義了你delegate具有類型LocationRequestModalViewController不符合LocationRequestModalViewControllerDelegate

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM