【问题标题】:No known class method for selector 'datePicker'选择器“datePicker”没有已知的类方法
【发布时间】:2015-09-14 00:14:59
【问题描述】:

我在这里搜索了类似的问题来帮助我解决我的问题,但都是徒劳的。我想要 UItextField 点击时拉出 UIDatePicker,然后用户可以选择一个日期,然后在 UITextfield 中设置它。但是,我不断收到“datePicker”错误。不知道我还应该做什么。

这是我的应用程序中调用 datePicker 的部分

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[pd resignFirstResponder]; //the textField that you will set the selected date
datePicker = [[UIDatePicker alloc] init]; //declared uidatepicker component

pickerViewDate = [[UIActionSheet alloc] initWithTitle:@"Select the date!"
                                             delegate:self
                                    cancelButtonTitle:nil
                               destructiveButtonTitle:nil
                                    otherButtonTitles:nil];

datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
datePicker.datePickerMode = UIDatePickerModeDate; //set your spesific mode

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]]; //or another LocaleIdentifier instead of en_US
[dateFormatter setDateFormat:@"dd.MM.yyyy"]; //desired format

[datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged]; //the function would be fired when user change the date in datePicker

//now preparing the toolbar which will be displayed at the top of the datePicker
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle=UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonClicked)]; //barbutton item is "DONE" and doneButtonClicked action will be fired when user clicks the button.
[barItems addObject:flexSpace]; // set the left of the bar

[pickerToolbar setItems:barItems animated:YES];
[pickerViewDate addSubview:pickerToolbar];
[pickerViewDate addSubview:datePicker];
[pickerViewDate showInView:self.view];
[pickerViewDate setBounds:CGRectMake(0,0,320, 464)]; //you can change the position
}

-(IBAction)dateChanged{

NSDateFormatter *FormatDate = [[NSDateFormatter alloc] init];
[FormatDate setLocale: [[NSLocale alloc]
                        initWithLocaleIdentifier:@"en_US"]];
[FormatDate setDateFormat:@"dd.MM.yyyy"];
pd.text = [FormatDate stringFromDate:[UIDatePicker datePicker]];
}

【问题讨论】:

  • 哪一行报错了?
  • 请发布错误。但是这一行似乎是错误的 pd.text = [FormatDate stringFromDate:[UIDatePicker datePicker]];
  • @DuncanC 提到的 madmik3 行是引发错误的行

标签: ios objective-c datepicker uitextfield uidatepicker


【解决方案1】:

您将无法通过 Google 搜索代码中错误的原因。您需要弄清楚,因为您的代码是独一无二的。

正如@madmik3 指出的那样,这一行是错误的:

pd.text = [FormatDate stringFromDate:[UIDatePicker datePicker]];

该代码试图调用 UIDatePicker 类方法“datePicker”,这没有任何意义。

您可能想要的是:

pd.text = [FormatDate stringFromDate: [datePicker date]];

那个代码在变量datePicker 中询问您的日期选择器对象的日期属性(日期选择器中设置的当前日期。

【讨论】:

  • 谢谢!有效。对于这个愚蠢的问题,我很抱歉,我还是个初学者,iOS 明智
  • 很高兴我能帮上忙。将来请务必提供完整、准确的错误消息以及导致它的特定行。我看了看,没有找到违规行。幸运的是 madmik3 找到了。
猜你喜欢
  • 2013-11-21
  • 2014-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多