3
-(IBAction) changeColOnClick:(id)sender
{
    NSArray *colors = [NSArray arrayWithObjects:@"[UIColor brownColor]",@"[UIColor blackColor]",@"[UI greenColor]",@"[UIColor redColor]", nil];

    self.view.backgroundColor = [colors objectAtIndex:i];
    // ERROR::changebackground[1089:207] -[NSCFString CGColor]: unrecognized selector sent to instance 0x357c

    //label.text = [colors objectAtIndex:i]; //i is defined in the implementation
    i++;

    if(i==[colors count]-1)
    {
        i=0;
    }
}
4

6 回答 6

2

您的代码正在崩溃,因为您将字符串保存到数组中。尝试将实际颜色保存到数组中,如下所示:

NSArray *colors = [NSArray arrayWithObjects:[UIColor brownColor], [UIColor blackColor], [UIColor greenColor], [UIColor redColor], nil];

self.view.backgroundColor = [colors objectAtIndex:i];

如果这仍然不起作用,您可能需要执行以下操作:

self.view.backgroundColor = ((UIColor *)[colors objectAtIndex:i]).CGColor;
于 2012-11-20T13:13:25.393 回答
2

@"[UIColor brownColor]"in arrayare 类型NSStringnot UIColor

所以array应该是这样的:

 NSArray *colors = [NSArray arrayWithObjects:[UIColor brownColor],[UIColor blackColor],[UI greenColor],[UIColor redColor], nil];

添加这样的单个实例MutableArray

 [colors addObject:[UIColor blackColor]];

很快:

于 2012-11-20T13:14:30.243 回答
2

像这样修改你的代码。它会工作

-(IBAction) changeColOnClick:(id)sender
{
    NSArray *colors = [NSArray arrayWithObjects:[UIColor brownColor],[UIColor blackColor],[UI greenColor],[UIColor redColor], nil];

    self.view.backgroundColor = [colors objectAtIndex:i];
    //label.text = [colors objectAtIndex:i]; //i is defined in the implementation
    i++;

    if(i==[colors count]-1)
    {
        i=0;
    }
 }
于 2012-11-20T13:15:12.987 回答
1

您将颜色添加到字符串对象之类的数组中。那是不对的。你应该这样做:

[colors addObject:[UIColor blackColor]];
[colors addObject:[UIColor redColor]];

等等

于 2012-11-20T13:12:42.853 回答
1

嘿,伙计,我只是测试您的代码,而您只是这样设置方法:-

int i;
-(IBAction) changeColOnClick:(id)sender
{

    NSArray *colors = [NSArray arrayWithObjects:@"redColor",@"blackColor",@"greenColor",@"redColor", nil];


    NSString *str =[colors objectAtIndex:i];
    i++;
   //DARK_BACKGROUNDNavigation=str;

    SEL blackSel = NSSelectorFromString(str);
    UIColor* tColor = nil;
    if ([UIColor respondsToSelector: blackSel])
        tColor  = [UIColor performSelector:blackSel];
    [self.view setBackgroundColor:tColor];

    if(i==[colors count]-1){i=0;}
}

它的工作快乐编码:)

下载它的演示

http://www.sendspace.com/file/9e68jx

于 2012-11-20T13:21:32.290 回答
1

您已将数组中的对象作为字符串,因此它给出了数组。你必须采取 UIColor 类型的对象..  

    UIColor *color1 = [UIColor brownColor];
    UIColor *color2 = [UIColor blackColor];
    UIColor *color3 = [UIColor greenColor];
    UIColor *color4 = [UIColor redColor];
    NSArray *colors = [NSArray arrayWithObjects:color1,color2,color3,color4, nil];
    self.view.backgroundColor = [colors objectAtIndex:i];
于 2012-11-20T13:23:27.353 回答