0

みんな。iphoneアプリを開発しています。

コードでボタンのタイトルを変更しようとしています。ボタンをクリックすると、タイトルが他のテキストを変更します。

だから、私は setTitle メソッドを使用しますが、アプリは強制終了されます。

コードは次のとおりです。

-(IBAction)deleteProject:(id)sender{

    UIButton *button = (UIButton *) sender;
    if (addButton.enabled == YES ){

        addButton.enabled = NO ;

        //Here, the code is stopped.
        [button setTitle:@"Back" forstate:UIControlStateNormal];
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        [mainTableView setEditing:YES animated:YES];
    }

    else{

        addButton.enabled = YES;

        //Here, the code is stopped. V
        [button setTitle:@"Delete" forstate:UIControlStateNormal];
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        [mainTableView setEditing:NO animated:YES];
    }
}

私を助けてください。

4

3 に答える 3

3

senderメソッドにパラメーターが渡されますdeleteProject:。このパラメーターは typeidで、何でもかまいません。

にキャストしてUIButton、 として使用しようとしますUIButton

あなたが得る例外は[UIBarButtonItem setTitle:forState:]

これは、それがまったくでsenderはないことを示しているはずです:)UIButtonUIBarButtonItem

これをテストできますが、このコードをdeleteProject:メソッドの先頭に置きます。

if (NO == [sender isKindOfClass:[UIButton class]]) {
    NSLog(@"Oh no, sender isn't a button, it's a %@", [sender class]);
}
于 2012-05-31T11:04:07.960 に答える
0

UIButton クラスの setTitle:forState メソッドのスペルが間違っています。便宜上、以下のスペルを修正しました。

 -(IBAction)deleteProject:(id)sender{

    UIButton *button = (UIButton *) sender;
    if (addButton.enabled == YES ) // you could also use if (addButton.enabled)
    {

        addButton.enabled = NO ;

        [button setTitle:@"Back" forState:UIControlStateNormal];

        [mainTableView setEditing:YES animated:YES];
    }

    else // addButton.enabled == NO
    {

        addButton.enabled = YES;

        [button setTitle:@"Delete" forState:UIControlStateNormal];

        [mainTableView setEditing:NO animated:YES];
    }
 }
于 2012-05-31T11:09:14.223 に答える
0

大文字のSsetTitle:forState:

また、==比較、=割り当てに使用します。代入は常にtrue.

于 2012-05-31T10:59:59.050 に答える