开发者

Cocoa Touch - how to assign value from UIDatePicker to a TextField in a TableView

Hihi all,

I have a fixed row tableView with several TextFields in it for editing purposes. 2 of the fields are for date input. I can call out the datePicker when focus on the 2 fields, but how can I set the selected date into the correspondent TextField?

In my datePickerValueChanged method, how can I know whic开发者_运维知识库h TextField is currently being edited with the UIDatePicker?

Please help. Great thanks in advance!

:)


you can try this: in .h file take a variable of UITextField *activeTextField now in

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
     //Give reference of textField to active one
     activeTextField = textField;

     return YES;
}

now you can have active text field. Now you can manipulate it in datePickerValueChanged method

Hope it helps you.


You can add a selector to your UIDatePickers as they will have different instance variables.

[datePicker addTarget:self
            action:@selector(changeDateInLabel1:)
         forControlEvents:UIControlEventValueChanged];

[datePicker2 addTarget:self
                action:@selector(changeDateInLabel2:)
             forControlEvents:UIControlEventValueChanged];

Then in the method you can do like this :

    - (void)changeDateInLabel2:(id)sender{
        //Use NSDateFormatter to write out the date in a friendly format
        NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
        _dateFormatter.dateStyle = NSDateFormatterMediumStyle;

//Or a text field
        label2.text = [NSString stringWithFormat:@"%@",
                     [_dateFormatter stringFromDate:datePicker2.date]];
        [_dateFormatter release];
    }   

For textField's you can use textFieldShouldBeginEditing or textFieldShouldBeginEditing to check which one you are on.


set the cell which contains the text field as selected. while setting the date, set it in the text filed of the cell which is selected.

this is how Apple prefers doing it. check Date Cell code sample.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜