2011-06-29 2 views
2

私の質問に対する回答が見つかりましたが、それでも機能しません。私はIBでボタンを設定しました。アクションとターゲットをコードで設定したいのです(学習のためだけです)。私はこれを行うのviewDidLoad機能でUI要素のsetActionとsetTargetが機能しない

- (IBAction) onSpecialCase:(id)sender { 
    NSLog(@"It's working"); 
} 

:私は私のMainViewControllerで機能を持っている

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
// Do any additional setup after loading the view from its nib. 
[myButton setTarget:self]; 
[myButton setAction:@selector(onSpecialCase:)]; 
} 

Xcodeはすでに私に言って、これらの行をマーク: "UIButtonは 'がsetTarget' に応答しないこと"。

-[UIRoundedRectButton setTarget:]: unrecognized selector sent to instance 0x4b47e30 
2011-06-29 08:12:22.465 FirstTry[3765:207] *** Terminating app due to uncaught exception  'NSInvalidArgumentException', reason: '-[UIRoundedRectButton setTarget:]: unrecognized selector sent to instance 0x4b47e30' 

私はXCodeの警告に期待できるものだが、その理由であること:とにかくアプリを実行しているとき、それは言って終了しますか?私がWeb上で見つけたすべての例はまさに私のものと似ています。 IBを介してボタンにactionMethodeを追加すると、期待どおりに動作します。私は間違って何をしていますか?

答えて

9

あなたがボタンにコンセントを接続するか、私は通常、何をすべきかを使う場合は、2番目のあなたがチェックする必要があり、出口としてボタンを宣言した場合は、最初のチェックは、コード

UIButton *Button=[UIButton buttonWithType:UIButtonTypeCustom] ; 
Button.frame=CGRectMake(0,0,30,30); // place your button 
[Button addTarget:self action:@selector(processButtonCLick) forControlEvents:UIControlEventTouchUpInside]; 
[Button setImage:[UIImage imageNamed:@"imageoricon.png"] forState:UIControlStateNormal]; 
[self.view addSubview:Button]; 
+0

ありがとう!コードは古いスタイルで投稿されていますか? setTargetとsetActionは古いiOSバージョンからですか?あなたがそれをやってくれると言っているウェブ上の多くの場所。失われたforStateは私の注意を引き上げたはずです。あなたの迅速な助けをありがとう。 – malthoff

+0

アクションとターゲットの状態を定義する必要があります – user784625

2

お手伝いがあります。

UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [sendButton setTitle:@"Send" forState:UIControlStateNormal]; 

    [sendButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12]]; 
    sendButton.frame = CGRectMake(0, 3, 67, 37); 
    [sendButton addTarget:self action:@selector(sendButtonClicked) forControlEvents:UIControlEventTouchUpInside]; 
+0

を新しいボタンを追加することですこちらこそありがとう。! – malthoff

関連する問題