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:方法中。这个参数是 type id,可以是任何东西。

您将其转换为UIButton并尝试将其用作UIButton.

你得到的例外是[UIBarButtonItem setTitle:forState:]

这应该告诉你这sender根本不是一个UIButton,它是一个UIBarButtonItem:)

您可以对此进行测试,但将此代码放在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

大写字母SsetTitle:forState:

此外,==用于比较,=用于分配。作业总是true.

于 2012-05-31T10:59:59.050 回答
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 回答