这是代码:
@interface CreateAccountViewController : UIViewController<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
{
UIToolbar *keyboardToolbar;
NSDate *birthdate;
UIActionSheet *dateSheet;
}
@property(strong, nonatomic) IBOutlet UITextField *txtFirstName;
@property(strong, nonatomic) IBOutlet UITextField *txtLastName;
@property(strong, nonatomic) IBOutlet UITextField *txtGender;
@property(strong, nonatomic) IBOutlet UITextField *txtBirthdate;
@property(strong, nonatomic) IBOutlet UITextField *txtEmail;
-(void)resignKeyboard:(id)sender;
-(void)gotoPreviousField:(id)sender;
-(void)gotoNextField:(id)sender;
-(void)setBirth;
-(void)dismissDateSet;
-(void)gotoPreviousBirthdate;
-(void)gotoNextBirthdate;
@end
@implementation CreateAccountViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[[[keyboardToolbar items] objectAtIndex:0] setEnabled:YES];
[[[keyboardToolbar items] objectAtIndex:1] setEnabled:YES];
if([textField isEqual:self.txtFirstName])
{
[[[keyboardToolbar items] objectAtIndex:0] setEnabled:NO];
}
else if([textField isEqual:self.txtEmail])
{
[[[keyboardToolbar items] objectAtIndex:0] setEnabled:NO];
}
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if([textField isEqual:self.txtBirthdate])
{
[self setBirth];
return NO;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view
if(keyboardToolbar==nil)
{
keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UIBarButtonItem *btnPrevious = [[UIBarButtonItem alloc] initWithTitle:@"Previous" style:UIBarButtonItemStyleBordered target:self action:@selector(gotoPreviousField:)];
UIBarButtonItem *btnNext = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(gotoNextField:)];
UIBarButtonItem *btnExtraSpace= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *btnDone=[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(resignKeyboard:)];
[keyboardToolbar setItems:[[NSArray alloc] initWithObjects:btnPrevious, btnNext, btnExtraSpace, btnDone, nil]];
}
}
-(void) resignKeyboard:(id)sender
{
if([self.txtFirstName isFirstResponder])
{
[self.txtFirstName resignFirstResponder];
}
else if([self.txtLastName isFirstResponder])
{
[self.txtLastName resignFirstResponder];
}
else if([self.txtGender isFirstResponder])
{
[self.txtGender resignFirstResponder];
}
else if([self.txtBirthdate isFirstResponder])
{
[self.txtBirthdate resignFirstResponder];
}
else if([self.txtEmail isFirstResponder])
{
[self.txtEmail resignFirstResponder];
}
}
-(void) gotoPreviousField:(id)sender
{
if([self.txtFirstName isFirstResponder])
{
[self.txtEmail becomeFirstResponder];
}
else if([self.txtLastName isFirstResponder])
{
[self.txtFirstName becomeFirstResponder];
}
else if([self.txtGender isFirstResponder])
{
[self.txtLastName becomeFirstResponder];
}
else if([self.txtEmail isFirstResponder])
{
[self.txtBirthdate becomeFirstResponder];
}
}
-(void)gotoNextField:(id)sender
{
if([self.txtFirstName isFirstResponder])
{
[self.txtLastName becomeFirstResponder];
}
else if([self.txtLastName isFirstResponder])
{
[self.txtGender becomeFirstResponder];
}
else if([self.txtGender isFirstResponder])
{
[self.txtBirthdate becomeFirstResponder];
}
else if([self.txtEmail isFirstResponder])
{
[self.txtFirstName becomeFirstResponder];
}
}
-(void)setBirth
{
dateSheet=[[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[dateSheet setActionSheetStyle:UIActionSheetStyleDefault];
CGRect pickerFrame=CGRectMake(0, 44, 0, 0);
UIDatePicker *birthDayPicker=[[UIDatePicker alloc] initWithFrame:pickerFrame];
[birthDayPicker setDatePickerMode:UIDatePickerModeDate];
[dateSheet addSubview:birthDayPicker];
UIToolbar *birthDayToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, dateSheet.bounds.size.width, 44)];
[birthDayToolbar setBarStyle:UIBarStyleBlackTranslucent];
[birthDayToolbar sizeToFit];
UIBarButtonItem *btnPrevious = [[UIBarButtonItem alloc] initWithTitle:@"Previous" style:UIBarButtonItemStyleBordered target:self action:@selector(gotoPreviousBirthdate:)];
UIBarButtonItem *btnNext = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(gotoNextBirthdate:)];
UIBarButtonItem *btnExtraSpace= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *btnDone=[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissDateSet:)];
[birthDayToolbar setItems:[[NSArray alloc] initWithObjects:btnPrevious, btnNext, btnExtraSpace, btnDone, nil]];
[dateSheet addSubview:birthDayToolbar];
[dateSheet showInView:self.view];
[dateSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
-(void)dismissDateSet
{
NSArray *listOfViews = [dateSheet subviews];
for(UIView *subView in listOfViews)
{
if([subView isKindOfClass:[UIDatePicker class]])
{
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
self.txtBirthdate.text=[dateFormatter stringFromDate:[(UIDatePicker*)subView date]];
}
}
}
-(void)gotoPreviousBirthdate
{
[self dismissDateSet];
[self.txtGender becomeFirstResponder];
}
-(void)gotoNextBirthdate
{
[self dismissDateSet];
[self.txtEmail becomeFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CreateCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UITextField *textField = [[UITextField alloc] init];
textField.enablesReturnKeyAutomatically = YES;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.clearButtonMode=UITextFieldViewModeWhileEditing;
textField.delegate=self;
CGRect aRect=CGRectMake(10, 10.f, CGRectGetWidth(cell.bounds)-40.f, 30.f );
textField.frame = aRect;
if(indexPath.row==0)
{
textField.placeholder = @"First name";
self.txtFirstName=textField;
}
else if(indexPath.row==1)
{
textField.placeholder = @"Last name";
self.txtLastName=textField;
}
else if(indexPath.row==2)
{
textField.placeholder = @"Gender";
self.txtGender=textField;
}
else if(indexPath.row==3)
{
textField.placeholder = @"Date of birth";
self.txtBirthdate=textField;
}
else
{
textField.placeholder = @"Email";
self.txtEmail=textField;
}
[cell.contentView addSubview:textField];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
textField.inputAccessoryView=keyboardToolbar;
return cell;
}
@end
如果我按下日期选择工具栏上的“上一步”按钮,我会收到以下异常:
[CreateAccountViewController gotoPreviousBirthdate:]: unrecognized selector sent to instance
如果我按下日期选择工具栏上的“下一步”按钮,我会收到以下异常:
[CreateAccountViewController gotoNextBirthdate:]: unrecognized selector sent to instance
如果我按下日期选择工具栏上的完成按钮,我会收到以下异常:
[CreateAccountViewController dismissDateSet:]: unrecognized selector sent to instance
似乎我犯了同样的错误3次,但我无法弄清楚那是什么。 感谢
答案 0 :(得分:2)
您将目标选择器设置为:
@selector(gotoPreviousBirthdate:)
但你宣布它为:
-(void)gotoPreviousBirthdate;
删除选择器中的:@selector(gotoPreviousBirthdate)
,因为您没有参数,或将声明更改为-(void)gotoPreviousBirthday:(id)sender
。
然后重复另外两个。
答案 1 :(得分:0)
您正在调用方法,就好像它们是类方法一样,但它们在您的接口中被声明为实例方法。此外,选择器的调用也是错误的。
首先创建该类的实例:
CreateAccountViewController *vc = [[CreateAccountViewController alloc] init]; //Something like this
然后你可以调用实例方法:
[vc gotoPreviousBirthday]; //Note no trailing colon!
或者,如果您将方法声明为
+(void)gotoPreviousBirthdate;
注意签名中的+
- 然后您可以在代码显示时调用该方法。
这是Objective-C中一个非常基本的概念,所以我建议你先阅读文档。 Apple Objective-C Primer