2014-12-19 74 views
-1

我是新来的xcode iOS编程,我真的需要你的帮助,无法识别的选择器发送到实例的错误。下面是我的实现xcode无法识别选择器发送到实例错误

//.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元素后,我收到发送到实例无法识别选择。

UPDATE

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 
+1

向我们展示整个消息!它实际上获得了有用的信息! – 2014-12-19 02:42:57

+0

'[AppAccountController btnSave:forEvent:]' - 你显然没有正确实现btnSave。 – 2014-12-19 03:11:00

回答

2

的问题是btnSave:btnSave:forEvent:是两回事。

你的AppAccountController有一个btnSave:方法,所以它可以发送btnSave:消息。但它没有btnSave:forEvent:方法。不幸的是,这是您指定的每当按钮被按下时使用的选择器。该按钮试图将btnSave:forEvent:发送到您的AppAccountController,并且AppAccountController不知道该如何处理。所以它崩溃了。

最简单的解决方法是修复按钮,使其发送btnSave:而不是btnSave:forEvent:。 (在任何情况下,您几乎都不需要动作方法的双参数变体。)

+0

有趣的是,从来没有听说过您将事件作为参数发送的动作/目标(除了典型的“发件人”之外)。我见过的最接近的东西是'-tocuhesBegan:withEvent:'等等。我想知道你是如何使用Interface Builder进行设置的。 – 2014-12-19 03:18:33

+0

“从来没有听说过你还发过活动的行动/目标”那么,@尼古拉斯·米里亚,你很年轻,而且这个世界充满了奇迹。请参阅我的书中的讨论:http://www.apeth.com/iOSBook/ch11.html#_actions双参数表格实际上是完整的表格;有一个参数形式和一个零参数形式。 – matt 2014-12-19 03:25:12

+1

@NicolasMiari下一次你控制 - 从一个按钮拖到你的代码中,当你形成Action时,仔细看看弹出窗口:它实际上会询问你是否想要零个,一个或两个参数。 – matt 2014-12-19 03:26:49

相关问题