UI按钮图像更改为默认值

时间:2012-12-31 13:29:11

标签: ios uibutton

我有一个Uibutton,它的默认背景图像设置我在运行时更改其图像,但我想保留所选的图像。但每次我点击uibutton图像更改为默认值。这是代码

- (void)changeColorOnRuntTime:(UIColor *)color{

    [btnTmpForColorPicker setBackgroundImage:[self imageNamed:@"customColor.png" withTint:color] forState:UIControlStateNormal];
    [btnTmpForColorPicker setBackgroundImage:[self imageNamed:@"customColor.png" withTint:color] forState:UIControlStateHighlighted];

//    [btnTmpForColorPicker seti];
//    tmpColor = [UIColor alloc]ini;
    tmpColor = [color copy];
//    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
//    [defaults setValue:color forKey:@"CustomPreColor"];

[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:color] forKey:@"CustomPreColor"];

}

和btnHandler

if (popOverColorPicker.popoverVisible) {
                [popOverColorPicker dismissPopoverAnimated:YES];
            }
            else
            {
            sdViewController = [[SDColorPickerViewController alloc]init];
            sdViewController.color = [UIColor whiteColor];
            sdViewController.tag = 1;
            sdViewController.delegate = self;
        //    CGRect rect = CGRectMake(viewController.view.frame.origin.x, viewController.view.frame.origin.y, viewController.view.frame.size.width, 380);
            //   viewController.view.frame = rect;
                btnTmpForColorPicker = (UIButton*)sender;
            UIButton *senderbt = (UIButton*) sender;
                if (tmpColor) {
                    [btnTmpForColorPicker setBackgroundImage:[self imageNamed:@"customColor.png" withTint:tmpColor] forState:UIControlStateNormal];
                     [btnTmpForColorPicker setBackgroundImage:[self imageNamed:@"customColor.png" withTint:tmpColor] forState:UIControlStateHighlighted];
                }

     //       [popOverColorPicker setPopoverContentSize:CGSizeMake(viewController.view.frame.size.width, 380)];
            popOverColorPicker = [[UIPopoverController alloc]initWithContentViewController:sdViewController];
                [popOverColorPicker setPopoverContentSize:CGSizeMake(300, 390)];
            [popOverColorPicker presentPopoverFromRect:senderbt.frame inView:self permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

1 个答案:

答案 0 :(得分:1)

替换它:

[btnTmpForColorPicker setBackgroundImage:[self imageNamed:@"customColor.png" withTint:color] forState:UIControlStateHighlighted];

用这个:

[btnTmpForColorPicker setBackgroundImage:[self imageNamed:@"customColor.png" withTint:color] forState:UIControlStateHighlighted] || UIControlStateSelected];

将相同的更改应用于btnHandler按钮