如何使用颜色选择器方法在按钮上设置颜色? [英] How to set colour on a button from colour picker methods?

查看:125
本文介绍了如何使用颜色选择器方法在按钮上设置颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在for循环中生成一些按钮,每个按钮都有自己的标记。现在我想点击一个按钮并打开颜色选择器并设置按钮的颜色。

I generate some buttons in a for loop, each with its own tag. Now I want to tap on a button and open a colour picker and set the colour of the button.

我的问题是当我打开颜色选择器并选择颜色时它被设置在一个不同的按钮上。我想点击一个按钮,只选择点击按钮的颜色。

My problem is that when I open the colour picker and choose a color it gets set on a different button. I want to tap on a button and choose the colour for the tapped button only.

我使用的代码是:

for(int i=0;i<=5;i++){ 
    btnphoto=[[UIButton alloc]initWithFrame:CGRectMake(10,(30*i)+110,50,20)];    
    [btnphoto setTitle:@"Photo" forState:UIControlStateNormal];     
    [btnphoto setBackgroundColor:[UIColor redColor]]; 
    [btnphoto addTarget:self action:@selector(buttonPressPickColor:)
               forControlEvents:UIControlEventTouchUpInside];  
    btnphoto.tag=100 + i;
    [self.view addSubview:btnphoto];
}

-(IBAction)buttonPressPickColor:(UIButton*)sender
{   
    btnphoto.tag = sender.tag;
    NSLog(@"Btn photo Tag = %d",sender.tag);
    NEOColorPickerViewController *controller = [[NEOColorPickerViewController alloc] init];
    controller.delegate = self;
    controller.selectedColor = self.currentColor;
    controller.title = @"Color Picker";
    UINavigationController* navVC = [[UINavigationController alloc]initWithRootViewController:controller]; 
    [self presentViewController:navVC animated:YES completion:nil];
}

-(void)colorPickerViewController:(NEOColorPickerBaseViewController *)controller didSelectColor:(UIColor *)color 
{
    btnphoto.backgroundColor = color;  /// Important Line
    [controller dismissViewControllerAnimated:YES completion:nil];
}


推荐答案

最简单的事情就是拥有一个实例变量/属性并将所选按钮的标签设置为它并使用该标签获取正确的按钮实例...

Easiest thing would be to have a instance variable/property and set your selected button's tag to it and use that tag to get the correct instance of button...

例如:

在界面文件中:

@property (nonatomic, weak) int selectedTag;

以下是经过修改的代码,它将为您提供更多帮助

Below is the modified code which gonna help you further

-(IBAction)buttonPressPickColor:(UIButton*)sender{   
_selectedTag = sender.tag;
NSLog(@"Btn photo Tag = %d",sender.tag);
NEOColorPickerViewController *controller = [[NEOColorPickerViewController alloc] init];
controller.delegate = self;
controller.selectedColor = self.currentColor;
controller.title = @"Color Picker";
UINavigationController* navVC = [[UINavigationController alloc]initWithRootViewController:controller]; 
[self presentViewController:navVC animated:YES completion:nil]; 
}
-(void)colorPickerViewController:(NEOColorPickerBaseViewController *)controller didSelectColor:(UIColor *)color {
UIButton *selectedButton = (UIbutton *)[self.view viewWithTag:_selectedTag];
selectedButton.backgroundColor = color;  /// Important Line
[controller dismissViewControllerAnimated:YES completion:nil]; }

希望这会有所帮助......

Hope this helps...

这篇关于如何使用颜色选择器方法在按钮上设置颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆