自定义协议代表为零
Custom Protocol delegate is nil
我有一个奇怪的错误,我的代表为零,不知道在哪里检查。也许其他人有同样的问题。
我有这个 class 和协议:
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
}
}
触发计划通知后,我单击它并调用 didReceive
,即使已设置委托,self.delegate?.pushVC()
仍为零(来自 didSet
的消息已打印)。
扩展中的委托方法从未被触发,因为委托为 nil。
我还在每个 class 的 deinit
中打印了一些消息,以查看它是否以某种方式从内存中删除但它们没有,只有在我从 [=31 弹出它们之后=]堆栈。
添加
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")
我有一个奇怪的错误,我的代表为零,不知道在哪里检查。也许其他人有同样的问题。
我有这个 class 和协议:
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
}
}
触发计划通知后,我单击它并调用 didReceive
,即使已设置委托,self.delegate?.pushVC()
仍为零(来自 didSet
的消息已打印)。
扩展中的委托方法从未被触发,因为委托为 nil。
我还在每个 class 的 deinit
中打印了一些消息,以查看它是否以某种方式从内存中删除但它们没有,只有在我从 [=31 弹出它们之后=]堆栈。
添加
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")