2016-04-10 43 views
0

需要在設定的時間每天發送提醒。使用UIStepper將時間設置爲1到24小時的格式。修改後的代碼,但它仍然無法正常工作,不來通告使用UIStepper進行UILocalNotification的時間

var hour = 0.0 
let notification = UILocalNotification() 
@IBOutlet weak var remindStepper: UIStepper! 
@IBOutlet weak var remindLabel: UILabel! 
@IBOutlet weak var remindSwitch: UISwitch! 

在viewDidLoad中()

remindStepper.wraps = true 
remindStepper.value = hour 
remindStepper.minimumValue = 0 
remindStepper.maximumValue = 24 
if NSUserDefaults.standardUserDefaults().objectForKey("hour") != nil { hour = NSUserDefaults.standardUserDefaults().objectForKey("hour") as! Double } 

if NSUserDefaults.standardUserDefaults().objectForKey("remindOnOff") != nil { remindOnOff = NSUserDefaults.standardUserDefaults().objectForKey("remindOnOff") as! Bool 
} 
    if remindOnOff == true { 
     remindSwitch.on = true 
    } else { 
     remindSwitch.on = false 
    } 
    remindLabel.text = "\(Int(hour)):00" 

然後創建

func setEveryDayNotification(){ 

    let calendar = NSCalendar.currentCalendar() 
    var dateFire = NSDate() 
    let components = calendar.components(.Hour,fromDate: dateFire) 
    components.calendar = calendar 
    components.hour = Int(hour) 
    dateFire = calendar.dateFromComponents(components)! 

    notification.alertTitle = "alert title" 
    notification.alertBody = "alert body" 
    notification.soundName = UILocalNotificationDefaultSoundName 
    notification.alertAction = "Run the workout" 
    notification.timeZone = NSTimeZone.defaultTimeZone() 
    notification.fireDate = components.date 
    notification.repeatInterval = NSCalendarUnit.Day 
    UIApplication.sharedApplication().scheduleLocalNotification(notification) 
} 
@IBAction func remindStepper(sender: UIStepper) { 
    NSUserDefaults.standardUserDefaults().setObject(Int(sender.value), forKey: "hour") 
    NSUserDefaults.standardUserDefaults().synchronize() 
    remindLabel.text = "\(Int(sender.value).description):00" 
    UIApplication.sharedApplication().cancelLocalNotification(notification) 
    remindOnOff = false 
    remindSwitch.on = false 
    NSUserDefaults.standardUserDefaults().setBool(remindOnOff, forKey: "remindOnOff") 
} 
    @IBAction func remindSwitch(sender: UISwitch) { 
    if remindSwitch.on { 
     setEveryDayNotification() 
     remindOnOff = true 
     NSUserDefaults.standardUserDefaults().setBool(remindOnOff, forKey: "remindOnOff") 
    } else { 
     remindOnOff = false 
     UIApplication.sharedApplication().cancelLocalNotification(notification) 
     NSUserDefaults.standardUserDefaults().setBool(remindOnOff, forKey: "remindOnOff") 
    } 

} 

回答

0

您當前的代碼更改的一年爲0000這樣您的通知沒有在適當的時間設置

您可以檢查以下代碼,將當前代碼

let calendar = NSCalendar.currentCalendar() 
    var dateFire = NSDate() 
    let components = calendar.components(.Hour,fromDate: dateFire) 
    components.calendar = calendar 
    components.hour = Int(hour) 
    dateFire = calendar.dateFromComponents(components)! 

與下面的代碼

let calendar = NSCalendar.currentCalendar() 
calendar.timeZone = NSTimeZone.localTimeZone() 
let dateFire = NSDate() 
let newDate = calendar.dateBySettingHour(Int(hour), minute:00, second:00, ofDate: dateFire, options:NSCalendarOptions()) 

而在通知消防數據的時間讓它這樣

notification.fireDate = newDate 

如果你把這個代碼的本地通知應設置

注意:根據您當前的代碼您在每次更改步進器值時都會調用setEveryDayNotification,因此它會根據步進器值爲每個小時設置通知,而不是您應該製作單獨的按鈕,以獲取步進器當前值併爲通知設置通知僅當前選定小時。

+0

你能幫我實現我的任務嗎?謝謝 –

+0

@ArturSkachkov你是否試過我寫過的代碼,那應該對你有幫助? – HardikDG

+0

通知將觸發一次。進一步改變價值小時,什麼也沒有發生:( –