刚学会在我的.m文件中使用代码添加动作时,我的第一次尝试抛出了一个异常,并使应用程序崩溃。
这是一个身份验证脚本,我希望它能与我的web服务器通信。以下是相关代码(这都在.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文件中没有button/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'发布于 2012-01-08 20:09:02
你的解决方法可能很简单:
[loginButton addTarget:self action:@selector(authenticate:) forControlEvents:UIControlEventTouchUpInside];(在@selector中的authenticate名称后面添加一个冒号)。如果一个方法接受任何参数,那么它后面会有一个冒号,为了让运行时环境能够向它发送消息,这个冒号需要包含在操作目标的选择器中。
我希望这篇文章能对你有所帮助!
附注:如果添加一个
-(IBAction)authenticate:(id)sender添加到.h文件。
IBAction在这里是InterfaceBuilder-Action的缩写,所以如果你在XIB文件中构建接口,这实际上会有更多的帮助。因为您是通过编程来设置目标的,所以您可以只使用-(void) authenticate (不使用冒号,因为您的方法使用或不需要参数),但在您对Objective C编码和操作有了更全面的体验之前,请不要这样做。
https://stackoverflow.com/questions/8777346
复制相似问题