ios - Swift 中的协议(protocol)和委托(delegate)

标签 ios swift

我有两个 View Controller :“DiscoverViewController”和“LocationRequestModalViewController”。

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

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

我做了一些研究,发现委托(delegate)/协议(protocol)是最好的方法,所以我按照指南来实现它,但我留下了两个错误,无法弄清楚它们。

错误是:

在 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

但是您将委托(delegate)声明为:

var delegate:LocationRequestModalViewController? = nil

您需要将其更改为:

var delegate:DiscoverViewController? = nil

问题 2

同理,LocationRequestModalViewController没有向LocationRequestModalViewControllerDelegate确认,更改delegate声明。

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

相关文章:

ios - 属性如何与 "self"相关,反之亦然?

ios - SWIFT 中的 audioPlayerDidFinishPlaying 函数

iOS swift : ScrollView strict dragging

ios - CoreData - 使用获取的 Controller 的行数无效

objective-c - UITableViewCell 未从 Storyboard 中加载 subview

ios - Tableview 单元格内边距和间距

ios - 如何在容器 View 中的嵌入 TableView 中更改变量? swift

ios - 带有 NSTimeInterval 值数组的后续 WKInterfaceTime/NSTimer 事件

ios - 从节目转场返回标题时,UISearchBar 会短暂显示灰色背景

swift - 启动空的 Swift 字典数组