【发布时间】:2012-01-08 12:06:39
【问题描述】:
刚刚学习在我的 .m 文件中使用代码添加操作,我的第一次尝试引发异常并使应用程序崩溃。
这是一个身份验证脚本,我希望它可以与我的网络服务器通信。下面是相关代码(这个都在.m文件里,很简单的.h文件):
.h(头文件)文件:
#import <UIKit/UIKit.h>
@interface Login_ViewController : UIViewController <UITextFieldDelegate>
@end
.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];
}
也在 .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];
}
我正在使用带有自定义类 login_ViewController 的视图的情节提要,并且它根据帐户 ID 的本地设置检查成功加载。这个窗口只有在他们之前没有登录的情况下才会被拉起,所以我可以进行身份验证并获取该帐户 ID 以存储在本地。正如您在 viewDidLoad 部分中看到的那样,我仍在尝试以编程方式创建所有这些项目。
这是因为我在 .h 文件中没有按钮/IBAction 引用而失败,还是可以在 .m 文件中使用 addTarget 方法?
来自日志的崩溃信息:
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'
【问题讨论】:
-
只需在您的
addTarget:action:forControlEvents:方法中的authenticate之后添加一个冒号,您不需要IBAction,如果您以编程方式添加您的操作,void在这里就可以了。 :) -
为什么人们总是将语言/API 与工具/IDE 混为一谈?这不是 Xcode 问题。这是一个 Objective-C 或 Cocoa 问题。
标签: objective-c cocoa-touch uikit uibutton