图像不会更改返回到UIControlState之间的视图

时间:2012-06-01 07:34:23

标签: iphone objective-c ios sdk uibutton

我可以更改图像但是当我回到这个视图时,它会在我选择UIControlState时显示与UIControlStateNormal相同的图像。

-(IBAction) favorateImage:(UIButton*)sender{

    AppDelegate *obj=[[UIApplication sharedApplication]delegate];

    if ([sender isSelected]) {

        [sender setImage:[UIImage imageNamed:@"favrtes_uncheck.png"] forState:UIControlStateNormal];
        [sender setSelected:NO];

        NSMutableArray* array = [[[NSUserDefaults standardUserDefaults] objectForKey:@"favoriteItems"] mutableCopy];
        [array removeObject:[NSNumber numberWithInt:self.number]];

        [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"favoriteItems"];

        NSLog(@"%@", array);

        obj.country_main= array;
    } else 
    {
        [sender setImage:[UIImage imageNamed:@"favrtes_detail.png"] forState:UIControlStateSelected];
        [sender setSelected:YES];

        NSMutableArray* array= [[[NSUserDefaults standardUserDefaults] objectForKey:@"favoriteItems"] mutableCopy];

        [array addObject:[NSNumber numberWithInt:self.number]];

        [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"favoriteItems"];

        NSLog(@"%@", array);

        obj.country_main= array;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用UIControlStateHighlighted。

[sender setImage:[UIImage imageNamed:@"favrtes_detail.png"] forState:UIControlStateHighlighted];

您也可以通过将图像设置为突出显示状态来从界面构建器设置此项。