【发布时间】:2016-02-01 15:10:06
【问题描述】:
谁能帮助我如何使用 UISwitch 的状态来打开/关闭 Appdelegate.swift 中声明的本地通知?
Viewcontroller.swift:
@IBOutlet weak var switchButton: UISwitch!
var switchState = true
let switchKey = "switchState"
@IBAction func saveSwitchPressed(sender: AnyObject) {
NSUserDefaults.standardUserDefaults().setBool(switchButton.on, forKey: "switchState")
}
override public func viewDidLoad() {
super.viewDidLoad()
switchButton.on = NSUserDefaults.standardUserDefaults().boolForKey("switchState")
}
AppDelegate.swift
func handleRegionEvent(region: CLRegion) {
// Show an alert if application is active
if UIApplication.sharedApplication().applicationState == .Active {
if let message = notefromRegionIdentifier(region.identifier) {
if let viewController = window?.rootViewController {
showSimpleAlertWithTitle(nil, message: message, viewController: viewController)
}
}
} else {
// Otherwise present a local notification
let notification = UILocalNotification()
notification.alertBody = notefromRegionIdentifier(region.identifier)
notification.soundName = "Default";
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
}
}
【问题讨论】:
-
所以你想在
NSUserDefaults.standardUserDefaults().boolForKey("switchState")是false时禁用本地通知? -
没有关闭本地通知的标准方法,当
switchState为false时,您应该忽略它们。 -
是的,当 switchstate 为 false 时,应该禁用本地通知
标签: ios swift uilocalnotification uiswitch