简体   繁体   中英

UIDatePicker Event Changed

I write a simple iOS app just for learning, in my case I need to use a UIDatePicker and, after the user change the date, I need to write this date to an UITextField , here's the code:

.h File

@interface ExpenseViewController: UIViewController<UITextFieldDelegate>

@property (strong,nonatomic) IBOutlet UITextField *editTextDate;

- (void) updateTextFieldDate:(UIDatePicker *)pickerL;

@end

.m

@implementation ExpenseViewController

@synthesize ediTextDate;

- (void) viewDidLoad
{
    [super viewDidLoad]
    UIDatePicker *datePicker;
    datePicker = [[UIDatePicker alloc]init];
    [datePicker setDate:[NSDate date]];
    [datePicker setDatePickerMode:UIDatePickerModeDate];
    [datePicker removeTarget:self action:nil forControlEvents:UIControlEventValueChanged];
    [datePicker addTarget:self action:@selector(updateTextFieldDate:)     forControlEvents:UIControlEventValueChanged];
    [editTextDate setInputView:datePicker];

}

- (void) updateTextFieldDate:(UIDatePicker *)pickerL{
    UIDatePicker *picker=(UIDatePicker*)self.editTextdate.inputView;
    editTextDate.text=[NSString stringWithFormat:@"%@",picker.date];
}
@end

But I'm getting this error:

ExpenseViewController updateTextFieldDate:]:unrecognized selector sent to instance 0x8d395f0

Can anyone help me?

Thanks.

your code runs fine on my system, but I am getting exact same error when mistyping the method name in @selector(updateTextFieldDate:).

I know, stupid suggestion, but is it possible you have typo there in code, even if what you showed here doesn't have it? the typo in synthesize makes it look like you typed your class here instead of copy-pasting, so perhaps the xcode version has a typo in @selector part?

This is because textfield property editTextDate your are synthesizing is wrong as ediTextDate (means t is missing) so replace it with ** editTextDate**.

and replace your code

    UIDatePicker *picker=(UIDatePicker*)self.editTextdate.inputView; // wrong editTextdate , it is editTextDate

with this :

UIDatePicker *picker=(UIDatePicker*)self.editTextDate.inputView;

Then your code will work properly.

Hope it helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM