如何在UITableViewCell标签中保存DatePicker值

时间:2015-06-29 12:52:04

标签: swift datepicker

我想将DatePicker值保存在UITableViewCell的标签(名称为数据的标签)中。我有一个AddController来在UITableView中添加数据。在原点我已经为日期和小时设置了两个文本字段,但我想用DatePicker替换这两个字段,以便根据日期设置通知。

细胞

$(window).on("navigate", function (event, data) {
      var direction = data.state.direction;
      if (direction == 'back') {
          // do something
      }
      if (direction == 'forward') {
          // do something else
      }
});

型号:

import UIKit

class PillsCell: UITableViewCell {



@IBOutlet var nomeMedicina: UILabel!
//@IBOutlet var ora: UILabel!
@IBOutlet var data: UILabel!
@IBOutlet var dosaggio: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

AddController:

import UIKit

class PillsModel: NSObject, NSCoding {

var nomeMedicina :String!
//var data :String!
//var ora :String!
var dosaggio :String!
var dataFormattata : NSDate!

init(nomeMedicinaIn:String, dosaggioIn:String, dataFormattataIn:NSDate) {
    nomeMedicina = nomeMedicinaIn
    //ora = oraIn
    //data = dataIn
    dosaggio = dosaggioIn
    dataFormattata = dataFormattataIn
}

internal required init(coder aDecoder: NSCoder) {
    self.nomeMedicina = aDecoder.decodeObjectForKey("nomeMedicina") as! String
    //self.ora = aDecoder.decodeObjectForKey("ora") as! String
    //self.data = aDecoder.decodeObjectForKey("data") as! String
    self.dosaggio = aDecoder.decodeObjectForKey("dosaggio") as! String
    self.dataFormattata = aDecoder.decodeObjectForKey("dataFormattata") as! NSDate

}

func encodeWithCoder(encoder: NSCoder) {
    encoder.encodeObject(self.nomeMedicina, forKey: "nomeMedicina")
    //encoder.encodeObject(self.ora, forKey: "ora")
    //encoder.encodeObject(self.data, forKey: "data")
    encoder.encodeObject(self.dosaggio, forKey: "dosaggio")
    encoder.encodeObject(self.dataFormattata, forKey: "dataFormattata")


}

}

的UITableView:

import UIKit

class AddPillsController: UIViewController, UITextFieldDelegate, CameraManagerDelegate {

@IBOutlet var fieldNomeMedicina: UITextField!
//@IBOutlet var fieldData: UITextField!
//@IBOutlet var fieldOra: UITextField!
@IBOutlet var fieldDosaggio: UITextField!
@IBOutlet var myDatePicker: UIDatePicker!

var profilo: ProfiloModel!

override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector:"drawAShape:", name: "actionOnePressed", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector:"showAMessage:", name: "actionTwoPressed", object: nil)


    fieldNomeMedicina.delegate = self
    //fieldData.delegate = self
    //fieldOra.delegate = self

    myDatePicker.locale = NSLocale.currentLocale()

    // Vista accessoria per la tastiera
    var keyboardToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.bounds.size.width, 44))
    keyboardToolbar.barStyle = UIBarStyle.BlackTranslucent
    keyboardToolbar.backgroundColor = UIColor.redColor()
    keyboardToolbar.tintColor = UIColor.whiteColor()
    var flex = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    var save = UIBarButtonItem(title: "Fatto", style: UIBarButtonItemStyle.Done, target: fieldDosaggio, action: "resignFirstResponder")
    keyboardToolbar.setItems([flex, save], animated: false)
    fieldDosaggio.inputAccessoryView = keyboardToolbar
}


func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder() // chiudere la tastiera nei campi di testo
    return true
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func annulla(sender: UIButton) {
    dismissViewControllerAnimated(true, completion: nil) // chiude una modal
}


@IBAction func selezionaData(sender: UIDatePicker) {

    var tempo = myDatePicker.date

    var dataFormattata = NSDateFormatter.localizedStringFromDate(tempo, dateStyle: .MediumStyle, timeStyle: .MediumStyle)
}


@IBAction func salva(sender: UIButton) {
    if fieldNomeMedicina.text.isEmpty &&
        //fieldData.text.isEmpty &&
        //fieldOra.text.isEmpty &&
        fieldDosaggio.text.isEmpty{
            //alertView che avverte l'utente che tutti i campi sono obbligatori
            //return

    }

    var therapy = PillsModel(nomeMedicinaIn: fieldNomeMedicina.text,
        dosaggioIn : fieldDosaggio.text,
        dataFormattataIn : myDatePicker.date
        //dataIn: fieldData.text,
        //oraIn: fieldOra.text,
        )

    profilo.therapyArra.append(therapy)
    DataManager.sharedInstance.salvaArray()
    DataManager.sharedInstance.detail.pillsTable.reloadData()

    dismissViewControllerAnimated(true, completion: nil)

    var localNotification = UILocalNotification()
    localNotification.category = "FIRST_CATEGORY"
    localNotification.fireDate = NSDate(timeIntervalSinceNow: 5)
    localNotification.alertBody = "Take your medicine:" + " " + fieldNomeMedicina.text + " " + fieldDosaggio.text
    localNotification.timeZone = NSTimeZone.defaultTimeZone()
    localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1


    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)


}


func drawAShape(notification:NSNotification){
    var view:UIView = UIView(frame:CGRectMake(10, 10, 100, 100))
    view.backgroundColor = UIColor.redColor()

    self.view.addSubview(view)

}

func showAMessage(notification:NSNotification){
    var message:UIAlertController = UIAlertController(title: "A Notification Message", message: "Hello there", preferredStyle: UIAlertControllerStyle.Alert)
    message.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

    self.presentViewController(message, animated: true, completion: nil)

}


}

0 个答案:

没有答案