【问题标题】:UIButton "addTarget" crashes appUIButton“addTarget”使应用程序崩溃
【发布时间】: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


【解决方案1】:

您的解决方法可能很简单:

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

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

我希望这是有道理的,可以帮助你!

附言添加也无妨

-(IBAction)authenticate:(id)sender

到 .h 文件。

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

【讨论】:

  • 你先生赢得了我永远的感谢......我存在的祸根是语法!!!!如果允许,我会在 9 分钟内投票/回答这个问题。
  • 实际上我没有使用 xib 文件,因为我已经在情节提要中构建了整个应用程序。这是我第一次尝试调出故事板上但未链接或引用的页面(您知道,在故事板上看到的视图之间的关联/链接)。我非常高兴能够为登录视图提供标题和标识符,并能够动态加载它:)
  • 然后我们继续下一个危机,“我无法在身份验证 IBAction 中获取我在页面上动态创建的文本字段的值.. 耶!” ...学习曲线很烂:)
  • btw .. 通过将每个 UITextfield 的 UITextfield 添加到具有属性的 .h 文件并将它们合成到 .m 文件中来修复文本引用。现在很开心。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-10
  • 2012-08-18
相关资源
最近更新 更多