私の.mファイルにコードを含むアクションを追加するだけで、最初の試行で例外がスローされ、アプリがクラッシュする。UIButton "addTarget"がアプリをクラッシュする
これは私のウェブサーバと通信することを望む認証スクリプトです。
.H(ヘッダ)ファイル:
#import <UIKit/UIKit.h>
@interface Login_ViewController : UIViewController <UITextFieldDelegate>
@end
.M(実装)ファイル:
ここに関連するコード(これはすべての.mファイル、非常に単純な.Hファイルである)であります-(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_Vのカスタムクラスを持って見るとストーリーボードを使用しています:.mファイル(ボタンのIBAction)中も
- (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];
}
iewControllerを呼び出すと、アカウントIDのローカル設定のチェックに基づいて正常にロードされます。このウィンドウは以前にログインしていない場合にのみプルアップされるため、認証してそのアカウントIDをローカルに保存することができます。私はまだviewDidLoadセクションで見ることができるように、これらのすべてのアイテムをプログラムで作成することを試しています。
.hファイルにボタン/ IBAction参照がないため、または.mファイルのすべての場所でaddTargetメソッドを使用しても問題ありません。ログから
クラッシュ情報:
[loginButton addTarget:self action:@selector(authenticate:) forControlEvents:UIControlEventTouchUpInside];
(あなた@selector
にauthenticate
名の後にコロンを追加します):
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'はここでもOKです。 :) – Kjuly
なぜ人々は常にツール/ IDEと言語/ APIを混ぜるのですか?これはXcodeの質問ではありません。これはObjective-CまたはCocoaの質問です。 –