使用计时器每X秒设置本地通知
我试图通过设置新的本地通知每隔X秒,当一个Set
button被按下,当用户点击一个Stop
button时停止报警应用程序。
使用Timer()
,我目前能够设置本地通知,但它不会重复,因为我想。 它只是当我进入应用程序,并通过Home
button回到前一个屏幕时,重复。 而且,这样做只能重复一次。
有什么办法可以让用户只按一下Set
button,不pipe你是在应用程序中还是在应用程序之外,只有当用户按下Stop
button时,才会停止通知?
我的代码如下:
class LocalNotificationViewController: UIViewController { var timer = Timer() func runTimer() { timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: (#selector(self.setLocalNotification)), userInfo: nil, repeats: true) } @objc func setLocalNotification() { // timeEntered = time.text! // let hrMin = timeEntered.components(separatedBy: ":"); let content = UNMutableNotificationContent() content.title = "Test Local Notification" content.subtitle = "Testing..." content.body = "Testing..." content.badge = 1 content.sound = UNNotificationSound(named: "alarm.aiff"); trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false); let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger) UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) } var timeEntered: String = ""; var trigger: UNTimeIntervalNotificationTrigger? = nil; @IBOutlet var time: UITextField!; @IBAction func setNotification(_ sender: Any) { runTimer() } @IBAction func stopRepeat(_ sender: Any) { timer.invalidate() }
- iOS RTP现场audio接收
- XCode:为什么didFinishLaunchingWithOptions中的launchOptions总是为零?
- UIImagePickerController被解散时,只在iOS 6.0中将视图推到20像素
- 在Swift中inheritancePFObject
- 当使用UINavigationController时,我的控制器的viewWillAppear或viewDidAppear方法不会被调用
- iOS NSDateFormatter dateFromString只在一个实例中返回nil
- 在XCode中debugging:exception断点
- 如果我重写一个类的方法,有没有办法,我可以调用原来的方法(被覆盖的方法)?
- 块获取释放,而在NSDictionary(ARC)