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)
}
}
}
回答
//
// 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)! }
}
這是有用!但是,當我從公共擴展名NSDate得到錯誤時說:聲明只在文件範圍內有效。 @LeonardoSavioDabus – 2014-12-08 00:22:32
創建一個新的空swift文件並粘貼在那裏 – 2014-12-08 00:23:04
它是公共擴展NSDate位@LeonardoSavioDabus – 2014-12-08 00:24:30
如果你已經有了UIDatePicker
,所有你需要做的是從它抓住date
屬性,並使用它來設置fireDate
。
那麼我怎麼會這樣做呢?我可以讓日期選擇器顯示時間。現在我怎麼把時間放進fireDate? – 2014-12-07 22:01:20
localNotification.fireDate = myDatePiker.date – 2014-12-07 22:29:22
我試過了@LeonardoSavioDabus,由於某種原因,我收到了一個錯誤,請看下面的評論以瞭解我現在的情況,並告訴我如何更改它,謝謝。 – 2014-12-07 23:44:46
- 1. 迅速設置特定日期和時間的本地通知3
- 2. 迅速多個通知
- 3. Linux上的快速用戶空間進程間通知方法?
- 4. 迅速推送通知和App Store
- 5. 迅速對講推送通知
- 6. 通知警告禁止迅速
- 7. 推送通知不工作迅速
- 8. 迅速註冊iOS 7通知
- 9. 本地通知IOS 7迅速
- 10. 如何在後臺運行的任務作出通知,迅速
- 11. 迅速NSTimer用戶信息
- 12. 如何在特定時間通過javascript進行通知?
- 13. didRefreshRegistrationToken如果用戶不允許不會被調用的通知迅速
- 14. 設備上迅速
- 15. 迅速的時間和距離
- 16. 的NSDate設置時區中迅速
- 17. 在特定時間設置通知android
- 18. 將通知設置爲特定時間
- 19. 將通知設置爲特定時間
- 20. 推送通知的應用背景的iOS 9迅速
- 21. 與被殺應用的iOS推送通知 - 迅速
- 22. 迅速的UITableView設置rowWidth
- 23. NSButton顏色設定編程迅速
- 24. 迅速AVPlayerItemDidPlayToEndTimeNotification不行
- 25. 迅速method_exchangeImplementations不行
- 26. 顯示推送通知用戶已設置的時間
- 27. 設置用戶輸入本地通知的時間
- 28. 如何在應用程序的特定時間通知用戶
- 29. 如何讓應用程序迅速終止通知可可
- 30. 使用迅速
究竟什麼是你的問題?如果是「如何使用日期選擇器」,那裏有大量的教程資源可以幫助解決這個問題。 – dpassage 2014-12-07 21:53:08
不,很抱歉不清楚,但我的意思是我如何將日期選擇器連接到火災日期?我希望通知在當時每天都會出現。 – 2014-12-07 22:14:34