2014-12-07 18 views
0
import UIKit 
class MyView: UIViewController { 
    @IBOutlet var mySwitch: UISwitch! 
    @IBOutlet var myDatePicker: UIDatePicker! 
    func datePicker() { myDatePicker.datePickerMode = UIDatePickerMode.Date } 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view. 
     datePicker() 
    } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
    @IBAction func switchPressed(sender: AnyObject) { 
     if mySwitch.on{ 
      var localNotification:UILocalNotification = UILocalNotification() 
      localNotification.alertAction = "Open App" 
      localNotification.alertBody = "Here is your notification at 7:00 AM" 
      localNotification.fireDate = myDatePicker.date 
      localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay 
      UIApplication.sharedApplication().scheduleLocalNotification(localNotification) 
     } else { 
      var localNotification:UILocalNotification = UILocalNotification() 
      localNotification.alertAction = "Open App" 
      localNotification.alertBody = "This notification should not appear" 
      localNotification.fireDate = NSDate(timeIntervalSinceNow: 999999999999) 
      IApplication.sharedApplication().scheduleLocalNotification(localNotification) 
     } 
    } 
} 
+0

究竟什麼是你的問題?如果是「如何使用日期選擇器」,那裏有大量的教程資源可以幫助解決這個問題。 – dpassage 2014-12-07 21:53:08

+0

不,很抱歉不清楚,但我的意思是我如何將日期選擇器連接到火災日期?我希望通知在當時每天都會出現。 – 2014-12-07 22:14:34

回答

1
// 
// ViewController.swift 
// Combining Date and Time 
// 
// Created by Leonardo Savio Dabus on 08/12/2014. 
// Copyright (c) 2014 inDabusiness.com. All rights reserved. 
// 

import UIKit 

class ViewController: UIViewController { 

    // IBOutlet goes here 
    @IBOutlet var myDatePicker: UIDatePicker! 
    @IBOutlet var mySwitch: UISwitch! 

    // let = whatever goes here 
    // var = whatever goes here 
    var localNotification = UILocalNotification() // You just need one 
    var notificationsCounter = 0 

    // put your functions now 
    func datePicker()   { myDatePicker.datePickerMode = UIDatePickerMode.Date } 
    func datePickerDefaultDate() { myDatePicker.date = NSDate().xDays(+1)    } 
    func notificationsOptions() { 
     localNotification.timeZone = NSTimeZone.localTimeZone() 
     localNotification.repeatInterval = .CalendarUnitDay 
     UIApplication.sharedApplication().scheduleLocalNotification(localNotification) 
     localNotification.alertAction = "Open App" 
     localNotification.alertBody = "Here is the seven o'clock notification" 
     localNotification.soundName = UILocalNotificationDefaultSoundName 
     localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1 
     //  you may add arbitrary key-value pairs to this dictionary. 
     //  However, the keys and values must be valid property-list types 
     //  if any are not, an exception is raised. 
     // localNotification.userInfo = [NSObject : AnyObject]? 
    } 
    func toggleSwitch(){ 
     if mySwitch.on{ 
      localNotification.fireDate = myDatePicker.date.fireDate // combined date = picked Date + 7:00am time 
     } else { 
      localNotification.fireDate = NSDate(timeIntervalSinceNow: 999999999999) // will never be fired 
     } 
    } 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     datePicker() 
     datePickerDefaultDate() 
     notificationsOptions() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
    // here is where you place your IBActions 

    @IBAction func switchPressed(sender: AnyObject) { 
     toggleSwitch() 

    } 
} 

創建你的項目中的新雨燕的源文件把你的擴展

import Foundation 
public extension NSDate { 
    func xDays(x:Int) -> NSDate { 
     return NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value: x, toDate: self, options: nil)! 
    } 
    var day:   Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitDay,   fromDate: self).day   } 
    var month:   Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitMonth,   fromDate: self).month   } 
    var year:   Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear,   fromDate: self).year   } 
    var fireDate: NSDate { return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: 7, minute: 0, second: 0, nanosecond: 0)! } 
} 
+0

這是有用!但是,當我從公共擴展名NSDate得到錯誤時說:聲明只在文件範圍內有效。 @LeonardoSavioDabus – 2014-12-08 00:22:32

+0

創建一個新的空swift文件並粘貼在那裏 – 2014-12-08 00:23:04

+0

它是公共擴展NSDate位@LeonardoSavioDabus – 2014-12-08 00:24:30

0

如果你已經有了UIDatePicker,所有你需要做的是從它抓住date屬性,並使用它來設置fireDate

+0

那麼我怎麼會這樣做呢?我可以讓日期選擇器顯示時間。現在我怎麼把時間放進fireDate? – 2014-12-07 22:01:20

+0

localNotification.fireDate = myDatePiker.date – 2014-12-07 22:29:22

+0

我試過了@LeonardoSavioDabus,由於某種原因,我收到了一個錯誤,請看下面的評論以瞭解我現在的情況,並告訴我如何更改它,謝謝。 – 2014-12-07 23:44:46