gpt4 book ai didi

ios - 调用嵌入式 containerVC 方法时不调用父 VC 委托(delegate)方法

转载 作者:行者123 更新时间:2023-11-28 10:46:14 24 4
gpt4 key购买 nike

我有一个父 VC,其中嵌入了一个容器中的子 VC。两个 VC 都符合委托(delegate),但只调用子委托(delegate)方法。我怎样才能让两个 VC 的委托(delegate)方法都响应?我是否遗漏了容器 View 的委托(delegate)模式?在此先感谢您的帮助。

中心类:

public protocol BLEManagerDelegate: class {
func bLEManagerShowAlert(message: String)
}

public class BLEManager: NSObject {

static let sharedInstance = BLEManager()
weak var delegate: BLEManagerDelegate?

public func postMessage() {
delegate?.bLEManagerShowAlert(message: message)
}
}

父VC

class HomeVC: ContentViewController, BLEManagerDelegate {

var bLEManager = BLEManager.sharedInstance

override func viewWillAppear(_ animated: Bool) {
bLEManager.delegate = self
}

// delegate methods

func bLEManagerShowAlert(message: String) {
// THIS METHOD IS NOT GETTING CALLED
}
}

嵌入到 ParentVC 中的容器 View

class ChildVC: UITableViewController, BLEManagerDelegate {

var bLEManager = BLEManager.sharedInstance

override func viewWillAppear(_ animated: Bool) {
bLEManager.delegate = self

// delegate methods

func bLEManagerShowAlert(message: String) {
// This method IS getting called
}
}

最佳答案

您的delegate 属性一次只能保存对一个对象的引用。一旦您的 ChildVC 将自己设置为委托(delegate),parentVC 就不再是委托(delegate)。

如果你想通知多个对象,你可以使用 NotificationCenter

关于ios - 调用嵌入式 containerVC 方法时不调用父 VC 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48459284/

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