-1

Does anyone know how to update a UILabel with UIDatePicker?

I just saw this in Objective C, and I didn't understand, I'm new on this.

Thanks.

See my Code:

@IBOutlet weak var inicioTextField: UITextField!
{
    didSet
    {
        inicioTextField.delegate = self
    }
}
// CONFIGURAÇÃO DA DATA DO DATEPICKER

lazy var dateFormatter: NSDateFormatter =
{
    let df = NSDateFormatter()
    df.dateStyle = .ShortStyle
    df.timeStyle = .NoStyle
    return df
}()


lazy var datePicker: UIDatePicker =
{
    let dp = UIDatePicker()
    dp.datePickerMode = .Date
    dp.addTarget(self, action: Selector("datePickerValueChanged:"), forControlEvents: .ValueChanged)
    return dp
}()


lazy var toolbar: UIToolbar =
{
    let tb = UIToolbar()
    let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
    let dismissKeyboardButton = UIBarButtonItem(title: "OK", style: .Plain, target: self, action: Selector("dismissKeyboard"))
    tb.setItems([flexibleSpace, dismissKeyboardButton, flexibleSpace], animated: false)
    tb.sizeToFit()
    tb.barStyle = .Default
    tb.tintColor = UIColor(red: 220/255, green: 20/255, blue: 60/255, alpha: 1)
    return tb
}()


var inicioDate = NSDate()
{
    didSet
    {
        updateView()
    }
}

override func viewDidLoad()

{
    super.viewDidLoad()

    updateView()
}

// CONFIGURAÇÃO DOS DATEPICKER


func datePickerValueChanged(sender: UIDatePicker)
{
    inicioDate = datePicker.date
}

private func updateView()
{
    inicioTextField.text = dateFormatter.stringFromDate(inicioDate)
}

And final, inside extension delegate:

func textFieldShouldBeginEditing(textField: UITextField) -> Bool      
{
    textField.inputView = datePicker
    textField.inputAccessoryView = toolbar
    datePicker.date = inicioDate
    return true
}
  • Your question is too vague to answer. What is it you want to do? Have the user pick a date in a date picker and install a string version of that date into the label? – Duncan C Dec 07 '15 at 00:07
  • Hi, sorry for my question... I have a UITextField that have a UIDatepicker as InputView, when user select it, automatically populate the UITextField, but I want to update a UILabel too.. – Felipe Boechat Dec 07 '15 at 00:37
  • I update my post with some codes... – Felipe Boechat Dec 07 '15 at 00:51
  • This is how I'm updating UITextField, but I have no Idea how to update a UILabel... – Felipe Boechat Dec 07 '15 at 00:52
  • So post the code that you have, and the header that shows the outlets to your text field and you label. Also, you're using Swift 2.1? That's the beta version correct? (For most things I advise against using pre-release versions of developer tools. Only if you have a product in the app store or are developing an app that needs specific features from a beta version would I recommend using beta development tools/SDKs for your development.) – Duncan C Dec 07 '15 at 00:53
  • 1
    Possible duplicate of [Replacing UITextField input with a UIDatePicker](http://stackoverflow.com/questions/11065946/replacing-uitextfield-input-with-a-uidatepicker) – Stonz2 Dec 07 '15 at 13:19

2 Answers2

0

You need to add an outlet to your label, and then just add code in your updateView method that sets the text of the label as well as the text of your text field. (You should probably refactor it to store the date into a String first, then assign that string to both your text field and your label.)

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0
func textFieldDidEndEditing(textField: UITextField) {
    label.text = textField.text
}
Tim
  • 2,089
  • 1
  • 12
  • 21