我希望能够点击“从现在开始1分钟”或“从现在开始1小时”的按钮,然后关闭通知(推/横幅)。我该怎么做呢?
答案 0 :(得分:1)
使用以下命令
按照按钮单击设置计时器1分钟或1小时 NSTimer.scheduledTimerWithTimeInterval(time, target: self, selector: #selector(functionName), userInfo: nil, repeats: true)
然后在该功能中广播通知,如下所示
NSNotificationCenter.defaultCenter().postNotificationName("youFunctionName", object: nil)
要在其中接收通知的viewcontroller中的addobserver
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(youFunctionName), name: "youFunctionName", object: nil)
答案 1 :(得分:0)
在这里,您可以编写代码来安排本地通知:
//First register for User Settings if iOS8 or above
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
if #available(iOS 8, *) {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil))
}
...
return true
}
//Function to schedule the local notification
func scheduleNotificationWithInterval(secondsAfter:Double, notificationTitle title:String, notificationBody body:String) {
let fireDate = NSDate()
let localNotif = UILocalNotification()
localNotif.fireDate = fireDate.dateByAddingTimeInterval(secondsAfter)
localNotif.alertBody = body
if #available(iOS 8.2, *) {
localNotif.alertTitle = title
}
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
UIApplication.sharedApplication().scheduleLocalNotification(localNotif)
}
希望它有所帮助:]