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

标签 ios swift uilocalnotification uiswitch

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

相关文章:

iphone - 在模拟器中启动 iOS 应用程序时出现白色 View

c# - Microsoft Office 365 在 iOS 中开发

ios - UITableView、UISwipeActionsConfiguration、UIContextualAction - pullView 不在 View 层次结构中。这是一个 UIKit 错误

ios - PhoneGap/iOS LocalNotification 应用程序在点击时崩溃 "View"

ios - 应用程序关闭时如何处理localNotifications

ios - 如何创建通知触发器? [ swift ]

iphone - SSCollectionView 边距/间距

objective-c - Restkit 对象映射,同一对象被多次引用

ios - 如何在 ios 的 swift 语言中为具有其他类型输入的变量设置 setter?

ios - 当我缩放 GMaps 自定义标记 View 时更改位置