用户设置快速通知的时间

用户设置快速通知的时间

本文介绍了用户设置快速通知的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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()

    }
}

在项目中创建一个新的Swift源文件,将您的扩展名

create a new Swift Source file at your project to put your extensions

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)! }
}

这篇关于用户设置快速通知的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 19:08