UIButton"addTarget"崩溃应用程序

Sil*_*ger 2 cocoa-touch objective-c uibutton uikit

只是学习使用我的.m文件中的代码添加操作,我的第一次尝试会引发异常并导致应用程序崩溃.

这是一个身份验证脚本,我希望能与我的Web服务器通信.这是相关的代码(这都是.m文件,非常简单的.h文件):

.h(标题)文件:

#import <UIKit/UIKit.h>

@interface Login_ViewController : UIViewController <UITextFieldDelegate>

@end
Run Code Online (Sandbox Code Playgroud)

.m(实现)文件:

- (void)viewDidLoad {
    UILabel *emailLabel = [[UILabel alloc] initWithFrame:CGRectMake(31.0f, 75.0f, 256.0f, 20.0f)];
    emailLabel.text = @"Email Address";
    emailLabel.backgroundColor = [UIColor grayColor];
    emailLabel.textColor = [UIColor whiteColor];
    UITextField *emailField = [[UITextField alloc] initWithFrame:CGRectMake(31.0f, 100.0f, 256.0f, 32.0f)];
    emailField.delegate = self;
    emailField.placeholder = @"Enter email address";
    emailField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:emailLabel];
    [self.view addSubview:emailField];
    UILabel *passLabel = [[UILabel alloc] initWithFrame:CGRectMake(31.0f, 175.0f, 256.0f, 20.0f)];
    passLabel.text = @"Password";
    passLabel.backgroundColor = [UIColor grayColor];
    passLabel.textColor = [UIColor whiteColor];
    UITextField *passField = [[UITextField alloc] initWithFrame:CGRectMake(31.0f, 200.0f, 256.0f, 32.0f)];
    passField.delegate = self;
    passField.placeholder = @"Enter Password";
    passField.borderStyle = UITextBorderStyleRoundedRect;
    passField.secureTextEntry = YES;
    UIButton *loginButton = [[UIButton alloc] initWithFrame:CGRectMake(31.0f, 275.0f, 256.0f, 32.0f)];
    [loginButton setTitle:@"Login" forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(authenticate) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:passLabel];
    [self.view addSubview:passField];
    [self.view addSubview:loginButton];
    [self.view setBackgroundColor: [UIColor grayColor]];
    UIAlertView *noAccount = [[UIAlertView alloc] initWithTitle:@"Welcome to T-of-U" message:@"Please enter your TofU credentials." delegate:self cancelButtonTitle:@"continue" otherButtonTitles: nil];
    [noAccount show];
    [super viewDidLoad];
}
Run Code Online (Sandbox Code Playgroud)

同样在.m文件中(按钮的IBAction):

-(IBAction)authenticate:(id)sender{
    // This will be the validation code
    // if we validate we will set the default Setting with accountID here.
    UIAlertView *auth1 = [[UIAlertView alloc] initWithTitle:@"You clicked a button" message:@"Oh, do you want to login do you?" delegate:self cancelButtonTitle:@"Sure.. Why not!" otherButtonTitles: nil];
    [auth1 show];
}
Run Code Online (Sandbox Code Playgroud)

我正在使用一个带有View的Storyboard,它具有一个自定义类的login_ViewController,并且它会根据对帐户ID的本地设置的检查成功加载.如果之前没有登录,则只会启动此窗口,因此我可以进行身份​​验证并获取该帐户ID以在本地存储.我仍在尝试以编程方式创建所有这些项目,您可以在viewDidLoad部分中看到.

这是否失败,因为我没有.h文件中的按钮/ IBAction引用,或者是否可以使用addTarget方法,其中所有文件都在.m文件中?

日志中的崩溃信息:

2012-01-08 04:54:32.868 TofU-5[10452:10103] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Login_ViewController authenticate]: unrecognized selector sent to instance 0x6c94780'
Run Code Online (Sandbox Code Playgroud)

Mic*_*ann 8

您的修复可能很简单:

[loginButton addTarget:self action:@selector(authenticate:) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)

(authenticate在你的名字后添加一个COLON @selector).如果一个方法接受任何参数,它后面会有一个冒号,并且运行时环境能够向它发送消息,冒号需要包含在动作目标的选择器中.

我希望这有意义并帮助你!

ps它不会伤害添加

-(IBAction)authenticate:(id)sender
Run Code Online (Sandbox Code Playgroud)

到.h文件.

IBAction这是InterfaceBuilder-Action的缩写,所以如果你在XIB文件中构建接口,这实际上会有很多帮助.因为你编程设定目标,你可以只使用-(void) authenticate(没有冒号,因为你的方法使用或不需要参数),但直到你得到目标C代码和操作更全面的体验不这样做呢.