0

我有一个 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];
4

1 回答 1

1

替换这个:

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

有了这个:

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

并对 btnHandler 按钮应用相同的更改

于 2012-12-31T17:42:43.470 回答