gpt4 book ai didi

objective-c - UIButton "addTarget"崩溃应用程序

转载 作者:行者123 更新时间:2023-12-04 02:57:19 24 4
gpt4 key购买 nike

刚刚学习在我的 .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];
}

我正在使用一个带有 View 的 Storyboard,该 View 具有自定义类 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'

最佳答案

您的修复可能很简单:

[loginButton addTarget:self action:@selector(authenticate:) forControlEvents:UIControlEventTouchUpInside];

(在您的@selector 中的authenticate 名称后添加一个COLON)。如果一个方法采用任何参数,它后面会有一个冒号,为了运行时环境能够向它发送消息,该冒号需要包含在操作目标的选择器中。

我希望这有道理并能帮助您!

附注添加

-(IBAction)authenticate:(id)sender

到 .h 文件。

IBAction 这里是 InterfaceBuilder-Action 的缩写,所以如果您在 XIB 文件中构建界面,这实际上会帮助更多。因为您是以编程方式设置目标,所以您可以只使用 -(void) authenticate (没有冒号,因为您的方法使用或不需要参数),但是在获得更多之前不要这样做Objective C 编码和操作的整体经验。

关于objective-c - UIButton "addTarget"崩溃应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8777346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com