为什么我不能取消重复的 UILocalNotification?
Why can't I cancel a repeating UILocalNotification?
Ciao, tout le monde. 在我的 Swift 应用程序中,有一个 UILocalNotification
计划在一个方法中,该方法将在一段时间后调用UIViewController
已加载,并且每小时重复一次(使用 repeatInterval = .CalendarUnitHour
)。我将此 UILocalNotification
实例存储在 UIViewController
中的私有 属性 中,以便以后可以取消它。当用户按下UIViewController
中的某个按钮时,action方法会触发通知实例的取消,我用这段代码(代码也用在deinit
中的[=12] =]:
if let notification = notificationProperty {
UIApplication.sharedApplication().cancelLocalNotification(notification)
notificationProperty = nil
}
我在这里使用 if-let 语句来防止意外取消 nil
UILocalNotification
。但是即使取消通知后,通知仍然每小时出现一次。
那么为什么不能正确取消呢?谢谢!
系统拥有的通知是您作为字段持有的通知的副本。您无法取消您持有的通知实例,因为它不是系统中的通知实例。
您可以通过 cancelAllLocalNotifications
取消所有现有通知,或者通过 scheduledLocalNotifications
属性 遍历所有现有实例找到您要取消的通知。
Ciao, tout le monde. 在我的 Swift 应用程序中,有一个 UILocalNotification
计划在一个方法中,该方法将在一段时间后调用UIViewController
已加载,并且每小时重复一次(使用 repeatInterval = .CalendarUnitHour
)。我将此 UILocalNotification
实例存储在 UIViewController
中的私有 属性 中,以便以后可以取消它。当用户按下UIViewController
中的某个按钮时,action方法会触发通知实例的取消,我用这段代码(代码也用在deinit
中的[=12] =]:
if let notification = notificationProperty {
UIApplication.sharedApplication().cancelLocalNotification(notification)
notificationProperty = nil
}
我在这里使用 if-let 语句来防止意外取消 nil
UILocalNotification
。但是即使取消通知后,通知仍然每小时出现一次。
那么为什么不能正确取消呢?谢谢!
系统拥有的通知是您作为字段持有的通知的副本。您无法取消您持有的通知实例,因为它不是系统中的通知实例。
您可以通过 cancelAllLocalNotifications
取消所有现有通知,或者通过 scheduledLocalNotifications
属性 遍历所有现有实例找到您要取消的通知。