gpt4 book ai didi

ios - 如何使用 UISwitch 关闭本地通知?

转载 作者:行者123 更新时间:2023-11-30 13:44:32 25 4
gpt4 key购买 nike

任何人都可以帮助我如何使用 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)
}
}

最佳答案

这应该可以使用 targetAction 来完成

   override public func viewDidLoad() {
super.viewDidLoad()
switchButton.on = NSUserDefaults.standardUserDefaults().boolForKey("switchState")

//Set button to observe target action
switchButton.addTarget(self, action: "presentNotification", forControlEvents: UIControlEvents.ValueChanged)
}

func presentNotification() {
//Do notification stuff here

}

关于ios - 如何使用 UISwitch 关闭本地通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35133892/

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