UIButton导致崩溃

时间:2014-06-19 01:55:12

标签: ios objective-c uibutton

我在这里遇到了一些问题。我创建了一个按钮,希望它在点击时更改它的backgroundImage。它没有用,我收到SIGABRT错误。

代码如下:

- (IBAction)indicatedButton:(id)sender
{
if ([sender isSelected])
{
    [sender setBackgroundImage:[UIImage imageNamed:@"completeTickBox.png"]];
}
else
{
    [sender setBackgroundImage:[UIImage imageNamed:@"blankTickBox.png"]];
}

}

3 个答案:

答案 0 :(得分:0)

我想你想要:

[sender setBackgroundImage:[UIImage ....] forState:UIControlStateNormal];

以下是该方法的相关Apple文档:https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instm/UIButton/setBackgroundImage:forState

所以在你的情况下,它将是:

- (IBAction)indicatedButton:(id)sender
{

    if ([sender isSelected])
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"completeTickBox.png"] forState:UIControlStateNormal];
    }
    else
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"blankTickBox.png"] forState:UIControlStateNormal];
    }

}

假设“发件人”实际上是一个UIButton。

答案 1 :(得分:0)

你必须将发件人转换为UIButton,因为发件人是(id)而你想要的对象是UIButton所以 将此行放在代码UIButton *btn=sender;

- (IBAction)indicatedButton:(id)sender
{
    UIButton *btn= sender;
    if ([btn isSelected])
    {
        [btn setBackgroundImage:[UIImage imageNamed:@"completeTickBox.png"] forState:UIControlStateNormal];
    }
    else
    {
        [btn setBackgroundImage:[UIImage imageNamed:@"blankTickBox.png"] forState:UIControlStateNormal];
    }

}

答案 2 :(得分:0)

首先验证您是否获得了UIButton

-(IBAction indicatedButton:(id)sender
{
    if ([sender isKindOfClass:[UIButton class]])
    {
    }
}

惯例提示:对象属性的最佳实践是使用点表示法(isSelected是UIButton的BOOL属性)

-(IBAction indicatedButton:(id)sender
{
    if ([sender isKindOfClass:[UIButton class]])
    {
        if (sender.isSelected)
        {

        }
    }
}

如果您只是使用常规按钮,但您想要的是“切换”按钮(它是一个图像或不同的图像),那么您需要为按钮设置所选的状态属性,以便它-knows-它被触摸后被选中(反之亦然)

-(IBAction indicatedButton:(id)sender
{
    if ([sender isKindOfClass:[UIButton class]])
    {
        if (sender.isSelected)
        {
            [sender setSelected:NO]; // if we were selected, we won't be now
            UIImage *buttonImage = [UIImage imageNamed:@"completeTickBox.png"];
            [sender setImage:buttonImage forState:UIControlStateNormal];
        }
        else if (!sender.isSelected)
        {
            [sender setSelected:YES]; // if we aren't selected, we will be now
            UIImage *buttonImage = [UIImage imageNamed:@"blankTickBox.png"];
            [sender setImage:buttonImage forState:UIControlStateNormal];
        }
    }
}

如果您知道要使用按钮,而不是使用内省(检查类类型),您还可以将按钮发送方类型从id更改为UIButton。您无需将按钮重新连接到storyboard / nib

即可执行此操作
-(IBAction indicatedButton:(UIButton *)sender
{
        if (sender.isSelected)
        {
            [sender setSelected:NO]; // if we were selected, we won't be now
            UIImage *buttonImage = [UIImage imageNamed:@"completeTickBox.png"];
            [sender setImage:buttonImage forState:UIControlStateNormal];
        }
        else if (!sender.isSelected)
        {
            [sender setSelected:YES]; // if we aren't selected, we will be now
            UIImage *buttonImage = [UIImage imageNamed:@"blankTickBox.png"];
            [sender setImage:buttonImage forState:UIControlStateNormal];
        }
}