自定义协议委派为零



我有一个奇怪的错误,我的委托为零,不知道在哪里检查。也许其他人也有同样的问题。

我有这个类和协议:

protocol NavigationProtocol: class {
func pushVC()
}
class LocalNotification: NSObject, UNUserNotificationCenterDelegate {
let notificationCenter = UNUserNotificationCenter.current()
weak var delegate: NavigationProtocol? {
didSet {
print("was set")
}
}
.........
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {

self.delegate?.pushVC()
completionHandler()
}

在我的VC 中

class FinalDecisionViewController: UIViewController {
var localNotification: LocalNotification!

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

private func setupNotification() {
localNotification = LocalNotification()
localNotification.delegate = self
localNotification.scheduleNotification(title: "Loan Approval", message: "Congratulations! Your loan has been approved", type: "Loan Approval")
}

extension FinalDecisionViewController: NavigationProtocol {
func pushVC() {
canMoveToNextVC = true
}
}

触发日程通知后,我点击它,调用didReceiveself.delegate?.pushVC()在某种程度上为零,即使已经设置了委托(打印了来自didSet的消息(。

来自扩展的委托方法从未被触发,因为委托为nil。

我还在每个类的deinit中打印了一些消息,看看它是否以某种方式从内存中删除了,但它们并没有,只是在我从vc堆栈中弹出它们之后。

添加

weak var delegate: NavigationProtocol? {
didSet {
print("was set")
notificationCenter.delegate = self
}
}

localNotification = LocalNotification()
localNotification.delegate = self
localNotification.notificationCenter.delegate = localNotification
localNotification.scheduleNotification(title: "Loan Approval", message: "Congratulations! Your loan has been approved", type: "Loan Approval")

最新更新