DatePicker出现在多个文本字段中

时间:2015-07-27 15:40:21

标签: ios core-data nsdate

我无法弄清楚代码混淆的地方。出于某种原因,当我单击textFieldStartTime或textFieldEndtime时,它们都会更改以及更改textFieldDDate中的日期。

感谢任何帮助。

Extension.swift

import UIKit
import CoreData

class AddDiveLogViewController: UIViewController {


@IBOutlet weak var textFieldDiveNumber: UITextField!

@IBOutlet weak var textFieldDiveLocation: UITextField!
@IBOutlet weak var textFieldDDate: UITextField!
@IBOutlet weak var textFieldStartTime: UITextField!
@IBOutlet weak var textFieldEndTime: UITextField!

var divenumber: String = ""
var divelocation: String = ""
var ddate = NSDate()
var starttime = NSDate()
var endtime = NSDate()

var datePickerView: UIDatePicker!
var timePickerView: UIDatePicker!
var endtimePickerView: UIDatePicker!

var existingItem: NSManagedObject!

override func viewDidLoad() {
    super.viewDidLoad()

    if (existingItem != nil) {
        textFieldDiveNumber.text = divenumber
        textFieldDiveLocation.text = divelocation
        textFieldDDate.text = ddate.stringValue
        textFieldStartTime.text = starttime.starttimestringValue
        textFieldEndTime.text = endtime.endtimestringValue

                }

    // Do any additional setup after loading the view.

    // This sets up the Date Picker

    datePickerView = UIDatePicker()
    datePickerView.datePickerMode = UIDatePickerMode.Date


    var toolbar = UIToolbar(frame: CGRectMake(0, 0, datePickerView.frame.width, 44))
    let OKButton = UIBarButtonItem(title: "OK", style: .Plain, target: self, action: "OKButtonTapped:")

    toolbar.setItems([OKButton], animated: true)

    self.textFieldDDate.inputView = datePickerView
    self.textFieldDDate.inputAccessoryView = toolbar

    // This sets up the Time Picker

    timePickerView = UIDatePicker()
    timePickerView.datePickerMode = UIDatePickerMode.Time


    var timetoolbar = UIToolbar(frame: CGRectMake(0, 0, timePickerView.frame.width, 44))
    let TimeOKButton = UIBarButtonItem(title: "OK", style: .Plain, target: self, action: "OKButtonTapped:")

    timetoolbar.setItems([TimeOKButton], animated: true)

    self.textFieldStartTime.inputView = timePickerView
    self.textFieldStartTime.inputAccessoryView = timetoolbar

    // This sets up the End Time Picker

    endtimePickerView = UIDatePicker()
    endtimePickerView.datePickerMode = UIDatePickerMode.Time


    var endtimetoolbar = UIToolbar(frame: CGRectMake(0, 0, endtimePickerView.frame.width, 44))
    let endTimeOKButton = UIBarButtonItem(title: "OK", style: .Plain, target: self, action: "OKButtonTapped:")

    endtimetoolbar.setItems([endTimeOKButton], animated: true)

    self.textFieldStartTime.inputView = endtimePickerView
    self.textFieldStartTime.inputAccessoryView = endtimetoolbar





}

@IBAction func saveTapped(sender: AnyObject) {

    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    let contxt: NSManagedObjectContext = appDel.managedObjectContext!
    let en = NSEntityDescription.entityForName("Divelog", inManagedObjectContext: contxt)

    if (existingItem != nil) {
        existingItem.setValue(textFieldDiveNumber.text, forKey: "divenumber")

        existingItem.setValue(textFieldDiveLocation.text, forKey: "divelocation")
        existingItem.setValue(textFieldDDate.text.dateValue!, forKey: "ddate")
        existingItem.setValue(textFieldStartTime.text.starttimedateValue!, forKey: "starttime")
        existingItem.setValue(textFieldEndTime.text.endtimedateValue!, forKey: "endtime")

    } else {
        var newItem = Divelog(entity: en!, insertIntoManagedObjectContext: contxt)

        newItem.divenumber = textFieldDiveNumber.text
        newItem.divelocation = textFieldDiveLocation.text
        newItem.ddate = textFieldDDate.text.dateValue!
        newItem.starttime = textFieldStartTime.text.starttimedateValue!
        newItem.endtime = textFieldEndTime.text.endtimedateValue!

    }



    contxt.save(nil)

    self.navigationController?.popToRootViewControllerAnimated(true)
}

@IBAction func cancelTapped(sender: AnyObject) {
    self.navigationController?.popToRootViewControllerAnimated(true)
}

func OKButtonTapped(sender: UIBarButtonItem) {
    self.textFieldDDate.endEditing(true)
    self.textFieldDDate.text = datePickerView.date.stringValue

    self.textFieldStartTime.endEditing(true)
    self.textFieldStartTime.text = timePickerView.date.starttimestringValue

    self.textFieldEndTime.endEditing(true)
    self.textFieldEndTime.text = endtimePickerView.date.endtimestringValue

}


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




}

AddDiveLogViewController.swift

protected void butClose_Click(object sender, EventArgs e)    
{        
    UpdateErrDiv.Visible = false;
    Page.RegisterStartupScript("closeWindow", "window.close()", true);
}

3 个答案:

答案 0 :(得分:1)

您的代码中存在错误。您已使用textFieldStartTime textField:

设置结束时间选择器
self.textFieldStartTime.inputView
self.textFieldStartTime.inputAccessoryView

所以,而不是

// This sets up the End Time Picker

    endtimePickerView = UIDatePicker()
    endtimePickerView.datePickerMode = UIDatePickerMode.Time


    var endtimetoolbar = UIToolbar(frame: CGRectMake(0, 0, endtimePickerView.frame.width, 44))
    let endTimeOKButton = UIBarButtonItem(title: "OK3", style: .Plain, target: self, action: "OK3ButtonTapped:")

    endtimetoolbar.setItems([endTimeOKButton], animated: true)

    self.textFieldStartTime.inputView = endtimePickerView
    self.textFieldStartTime.inputAccessoryView = endtimetoolbar

你应该:

// This sets up the End Time Picker

    endtimePickerView = UIDatePicker()
    endtimePickerView.datePickerMode = UIDatePickerMode.Time


    var endtimetoolbar = UIToolbar(frame: CGRectMake(0, 0, endtimePickerView.frame.width, 44))
    let endTimeOKButton = UIBarButtonItem(title: "OK", style: .Plain, target: self, action: "OKButtonTapped:")

    endtimetoolbar.setItems([endTimeOKButton], animated: true)

    self.textFieldEndTime.inputView = endtimePickerView
    self.textFieldEndTime.inputAccessoryView = endtimetoolbar

它应该有效。

答案 1 :(得分:1)

第三个错误的第三个答案......

你的OKButton,TimeOKButton,endTimeOKButton触发相同的动作OKButtonTapped。

在OKButtonTapped函数中,初始化日期/开始时间和结束时间值。 问题是UIDatePicker的不同实例与每个textField inputView相关联。

因此,每次触发OKButtonTapped时,都会将textField值与每个UIDatePicker相关联,即当您选择开始时间时,结束时间将与当前日期/时间相关联(反之亦然)。

我的简单建议是为每个按钮设置三个不同的操作:

override func viewDidLoad() {
    super.viewDidLoad()

    if (existingItem != nil) {
        textFieldDiveNumber.text = divenumber
        textFieldDiveLocation.text = divelocation
        textFieldDDate.text = ddate.stringValue
        textFieldStartTime.text = starttime.starttimestringValue
        textFieldEndTime.text = endtime.endtimestringValue

                }

    // Do any additional setup after loading the view.

    // This sets up the Date Picker

    datePickerView = UIDatePicker()
    datePickerView.datePickerMode = UIDatePickerMode.Date


    var toolbar = UIToolbar(frame: CGRectMake(0, 0, datePickerView.frame.width, 44))
    let OKButton = UIBarButtonItem(title: "OK", style: .Plain, target: self, action: "OK1ButtonTapped:")

    toolbar.setItems([OKButton], animated: true)

    self.textFieldDDate.inputView = datePickerView
    self.textFieldDDate.inputAccessoryView = toolbar

    // This sets up the Time Picker

    timePickerView = UIDatePicker()
    timePickerView.datePickerMode = UIDatePickerMode.Time


    var timetoolbar = UIToolbar(frame: CGRectMake(0, 0, timePickerView.frame.width, 44))
    let TimeOKButton = UIBarButtonItem(title: "OK", style: .Plain, target: self, action: "OK2ButtonTapped:")

    timetoolbar.setItems([TimeOKButton], animated: true)

    self.textFieldStartTime.inputView = timePickerView
    self.textFieldStartTime.inputAccessoryView = timetoolbar

    // This sets up the End Time Picker

    endtimePickerView = UIDatePicker()
    endtimePickerView.datePickerMode = UIDatePickerMode.Time


    var endtimetoolbar = UIToolbar(frame: CGRectMake(0, 0, endtimePickerView.frame.width, 44))
    let endTimeOKButton = UIBarButtonItem(title: "OK", style: .Plain, target: self, action: "OK3ButtonTapped:")

    endtimetoolbar.setItems([endTimeOKButton], animated: true)

    self.textFieldEndTime.inputView = endtimePickerView
    self.textFieldEndTime.inputAccessoryView = endtimetoolbar

}

因此,您将拥有三种不同的功能:

func OK1ButtonTapped(sender: UIBarButtonItem) {
    self.textFieldDDate.endEditing(true)
    self.textFieldDDate.text = datePickerView.date.stringValue
}
func OK2ButtonTapped(sender: UIBarButtonItem) {
    self.textFieldStartTime.endEditing(true)
    self.textFieldStartTime.text = timePickerView.date.starttimestringValue        
}
func OK3ButtonTapped(sender: UIBarButtonItem) {
    self.textFieldEndTime.endEditing(true)
    self.textFieldEndTime.text = endtimePickerView.date.endtimestringValue        
}

这可能看似多余,但并不是因为最终每个按钮都包含在不同的InputView和DatePicker中。

答案 2 :(得分:0)

好的,我已经检查了你的项目,我发现了另一个错误(最后一个?;))。

在DiveLogTableViewController.swift prepareForSegue函数中,你忘了从CoreData设置starttime和endtime。

    ADLVC.divenumber = selectedItem.valueForKey("divenumber") as! String
    ADLVC.ddate = selectedItem.valueForKey("ddate") as! NSDate
    ADLVC.divelocation = selectedItem.valueForKey("divelocation") as! String

    // I added this, and it works fine...

    ADLVC.starttime = selectedItem.valueForKey("starttime") as! NSDate
    ADLVC.endtime = selectedItem.valueForKey("endtime") as! NSDate

尝试多睡一觉;)