简体   繁体   English

UIDatePicker事件已更改

[英]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: 我写一个简单的iOS应用程序只是为了学习,在我的情况下我需要使用UIDatePicker ,在用户更改日期后,我需要将此日期写入UITextField ,这里是代码:

.h File .h文件

@interface ExpenseViewController: UIViewController<UITextFieldDelegate>

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

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

@end

.m .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:). 你的代码在我的系统上运行正常,但是当在@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? 合成中的错字使得它看起来像你在这里键入你的类而不是复制粘贴,所以也许xcode版本在@selector部分有拼写错误?

This is because textfield property editTextDate your are synthesizing is wrong as ediTextDate (means t is missing) so replace it with ** editTextDate**. 这是因为你正在合成的textfield属性editTextDate是错误的ediTextDate (意味着缺少t ),所以用** 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. 希望它能帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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