我是xcode iOS编程的新手,我真的需要你帮助解决发送到实例的无法识别的选择器的错误。以下是我的实施
//.h file
@interface AppAccountController : UIViewController <UIAlertViewDelegate>{
IBOutlet UITextField *txtName;
IBOutlet UITextField *txtEmail;
IBOutlet UIDatePicker *dateOB;
IBOutlet UITextField *txtPwd;
IBOutlet UITextField *txtRePwd;
}
- (IBAction)btnSave:(id)sender;
@end
//.m file
@interface AppAccountController ()
@property (strong, nonatomic) IBOutlet UITextField *txtName;
@property (strong, nonatomic) IBOutlet UITextField *txtEmail;
@property (strong, nonatomic) IBOutlet UIDatePicker *dateOB;
@property (strong, nonatomic) IBOutlet UITextField *txtPwd;
@property (strong, nonatomic) IBOutlet UITextField *txtRePwd;
@property (strong, nonatomic) IBOutlet UIButton *btnSave;
@end
@implementation AppAccountController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)btnSave:(id)sender {
// Create strings to store the text info
NSString *name = [txtName text];
NSString *email = [txtEmail text];
NSDate *dob = [dateOB date];
NSString *passwd = [txtPwd text];
NSString *repasswd = [txtRePwd text];
if ([txtName.text isEqualToString:@""])
{
[txtName becomeFirstResponder];
UIAlertView* finalCheck = [[UIAlertView alloc]
initWithTitle:@"Attention"
message:@"Name Field is empty. Please enter a name"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil] ;
[finalCheck show];
finalCheck.tag = 1;
}
else if ([txtEmail.text isEqualToString:@""])
{
[txtEmail becomeFirstResponder];
UIAlertView* finalCheck = [[UIAlertView alloc]
initWithTitle:@"Attention"
message:@"Email Field is empty. Please enter an Email"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil] ;
[finalCheck show];
finalCheck.tag = 2;
}
else if ([txtPwd.text isEqualToString:@""])
{
[txtPwd becomeFirstResponder];
UIAlertView* finalCheck = [[UIAlertView alloc]
initWithTitle:@"Attention"
message:@"Password Field is empty. Please enter a Password"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil] ;
[finalCheck show];
finalCheck.tag = 3;
}
else if ([txtRePwd.text isEqualToString:@""])
{
[txtRePwd becomeFirstResponder];
UIAlertView* finalCheck = [[UIAlertView alloc]
initWithTitle:@"Attention"
message:@"Confirm Password Field is empty. Please enter to confirm Password"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil] ;
[finalCheck show];
finalCheck.tag = 4;
}
else if (![passwd isEqualToString:repasswd])
{
[txtPwd becomeFirstResponder];
UIAlertView* finalCheck = [[UIAlertView alloc]
initWithTitle:@"Attention"
message:@"Passwords do not match. Please enter to confirm Password"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil] ;
[finalCheck show];
finalCheck.tag = 5;
}
else{
// Store the data
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:name forKey:@"name"];
[defaults setObject:email forKey:@"email"];
[defaults setObject:passwd forKey:@"passwd"];
[defaults setObject:dob forKey:@"dob"];
[defaults synchronize];
NSLog(@"Data saved");
[self performSegueWithIdentifier:@"saveLoad" sender:self];
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if(alertView.tag == 1) {
[txtName becomeFirstResponder];
NSLog(@"Name AlertView is clicked");
}
else if(alertView.tag == 2) {
[txtEmail becomeFirstResponder];
NSLog(@"Email AlertView is clicked");
}
else if(alertView.tag == 3) {
[txtPwd becomeFirstResponder];
NSLog(@"Password AlertView is clicked");
}
else if(alertView.tag == 4) {
[txtRePwd becomeFirstResponder];
NSLog(@"Re-Password AlertView is clicked");
}
else if(alertView.tag == 5) {
[txtRePwd becomeFirstResponder];
NSLog(@"Re-Password AlertView is clicked");
}
}
@end
每当我按下btnSave按钮或移动到另一个UI元素后(一个元素在另一个元素丢失之后获得焦点),我将无法识别的选择器发送到实例。
更新
2014-12-18 18:06:23.794 XPB[964:35743] -[AppAccountController btnSave:forEvent:]: unrecognized selector sent to instance 0x7fd949d923f0
2014-12-18 18:06:23.834 XPB[964:35743] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppAccountController btnSave:forEvent:]: unrecognized selector sent to instance 0x7fd949d923f0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010bd2df35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010a182bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010bd3504d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010bc8d27c ___forwarding___ + 988
4 CoreFoundation 0x000000010bc8ce18 _CF_forwarding_prep_0 + 120
5 UIKit 0x00000001087e98be -[UIApplication sendAction:to:from:forEvent:] + 75
6 UIKit 0x00000001088f0410 -[UIControl _sendActionsForEvents:withEvent:] + 467
7 UIKit 0x00000001088ef7df -[UIControl touchesEnded:withEvent:] + 522
8 UIKit 0x000000010882f308 -[UIWindow _sendTouchesForEvent:] + 735
9 UIKit 0x000000010882fc33 -[UIWindow sendEvent:] + 683
10 UIKit 0x00000001087fc9b1 -[UIApplication sendEvent:] + 246
11 UIKit 0x0000000108809a7d _UIApplicationHandleEventFromQueueEvent + 17370
12 UIKit 0x00000001087e5103 _UIApplicationHandleEventQueue + 1961
13 CoreFoundation 0x000000010bc63551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14 CoreFoundation 0x000000010bc5941d __CFRunLoopDoSources0 + 269
15 CoreFoundation 0x000000010bc58a54 __CFRunLoopRun + 868
16 CoreFoundation 0x000000010bc58486 CFRunLoopRunSpecific + 470
17 GraphicsServices 0x000000010c64e9f0 GSEventRunModal + 161
18 UIKit 0x00000001087e8420 UIApplicationMain + 1282
19 XPB 0x00000001083965b3 main + 115
20 libdyld.dylib 0x000000010ce4b145 start + 1
21 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
答案 0 :(得分:2)
问题是btnSave:
和btnSave:forEvent:
是两回事。
您的AppAccountController有一个btnSave:
方法,因此可以发送btnSave:
消息。但它没有btnSave:forEvent:
方法。不幸的是,只要按下按钮,就会指定使用的选择器。该按钮正在尝试将btnSave:forEvent:
发送到您的AppAccountController,而AppAccountController并不知道如何处理它。所以它崩溃了。
最简单的解决方案是修复按钮,使其发送btnSave:
而不是btnSave:forEvent:
。 (在任何情况下,您几乎都不需要动作方法的双参数变体。)