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 に答える