gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:22:39 24 4
gpt4 key购买 nike

我有两个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com