gpt4 book ai didi

iOS Swift,协议(protocol)中有一个公共(public)方法,两个ViewControllers中有两个实现,如何确定调用哪个ViewControllers方法?

转载 作者:行者123 更新时间:2023-11-28 07:51:42 29 4
gpt4 key购买 nike

我有一个协议(protocol)

protocol HandleEmbedController: class {
func printMsg()
}

和 2 个容器 View 和 2 个相应的 ViewControllers

class EnemyBaseVC: UIViewController, HandleEmbedController {

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

var value1 = ""

func printMsg(){
print("printing some embedded message")
}

}

class EnemyBase2VC: UIViewController, HandleEmbedController {

func printMsg() {
print("enemy base 2 message")
}

override func viewDidLoad() {
super.viewDidLoad()

}

}

并且都使用协议(protocol) HandleEmbedController 并实现 printMsg 功能。

在我的主 ViewController 中

class HomeBaseVC: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

}

var handleEmbedController:HandleEmbedController?

@IBAction func onclick(_ sender: UIButton) {

handleEmbedController?.printMsg()
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if (segue.identifier == "embedseg"){
if let embed = segue.destination as? EnemyBaseVC {
self.handleEmbedController = embed
}
}

if (segue.identifier == "embedseg2"){
if let embed = segue.destination as? EnemyBase2VC {
self.handleEmbedController = embed
}
}


}
}

当按钮被点击时,EnemyBaseVC 方法总是被调用并打印

printing some embedded message

有什么方法可以决定调用哪个方法吗?

更新 Main storyboard layout

最佳答案

如果您有两个容器 View ,两个转场都将在加载时触发,并且 handleEmbedController 将引用最后加载的一个 ViewController。

如果您有一些逻辑来决定应该引用哪个,那么您可以使用它来决定将引用哪个 ViewController,如下所示:

class HomeBaseVC: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

}

var handleEmbedController:HandleEmbedController?

// comes from your decision logic
var decisionMaker: Bool = false

@IBAction func onclick(_ sender: UIButton) {

handleEmbedController?.printMsg()
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if (segue.identifier == "embedseg"),
let embed = segue.destination as? EnemyBaseVC,
decisionMaker {
self.handleEmbedController = embed
}

else if (segue.identifier == "embedseg2"),
let embed = segue.destination as? EnemyBase2VC,
!decisionMaker {
self.handleEmbedController = embed
}
}
}

请注意,这会在加载时设置 handleEmbedController,如果您需要更复杂的行为,您最好在 segue 之外的其他地方处理 handleEmbedController 的分配。

由于这是您的基础 ViewController 必须与多个对象通信的场景,您还可以使用通知而不是委托(delegate)。这样,您可以决定在用户​​点击按钮时发送哪条消息。您的基本 ViewController 看起来像这样

class HomeBaseVC: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

}

// comes from your decision logic
var decisionMaker: Bool = true

@IBAction func onclick(_ sender: UIButton) {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "BUTTON_WAS_TAPPED"), object: nil, userInfo: ["decision" : decisionMaker])
}
}

虽然敌人的 ViewController 看起来像这样(第二个除了要处理的决策值外是一样的)

class EnemyBaseVC: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "BUTTON_WAS_TAPPED"),
object: nil,
queue: nil) { notification in
if let userInfo = notification.userInfo,
let decision = userInfo["decision"] as? Bool,
decision {
self.printMsg()
}
}
}

var value1 = ""

private func printMsg(){
print("printing some embedded message")
}
}

关于iOS Swift,协议(protocol)中有一个公共(public)方法,两个ViewControllers中有两个实现,如何确定调用哪个ViewControllers方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49496885/

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