objective-c - iOS 颜色数组检索错误

标签 objective-c ios uiview colors

我正在尝试使用一个按钮来切换按钮的颜色。出于某种原因,下面的代码无法正常工作...

**//Header (.h)**
    @property UIColor *imageColor1;
    @property UIColor *imageColor2;    
    @property UIButton *button1;
    @property UIButton *button2;    
    @property CGRect viewBounds;
    -(IBAction)changeColor:(id)sender;    
    -(IBAction)createButton;

**//Implementation (.m)**
    @synthesize imageColor1;
    @synthesize imageColor2;
    @synthesize button1;
    @synthesize button2;
    @synthesize viewBounds;

    -(IBAction)createButton{
        self.viewBounds = self.view.bounds;

        self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button1.frame = CGRectMake(CGRectGetMidX(self.viewBounds)-30, CGRectGetMidY(viewBounds)-15, 60, 30); 
        self.button1.backgroundColor = self.imageColor1;
        [self.view addSubview:button1];

        self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button2.frame = CGRectMake(CGRectGetMidX(self.viewBounds)-15, CGRectGetMidY(viewBounds)-30, 30, 60); 
        self.button2.backgroundColor = self.imageColor2;          
        [self.view addSubview:button2];

    }

    -(IBAction)changeColor:(id)sender{

       int changeCount = 0;
       int changeCount1 = 1;
       NSArray *colors = [[NSArray alloc] initWithObjects:[UIColor redColor],[UIColor blueColor],[UIColor yellowColor],[UIColor                  magentaColor],[UIColor greenColor],[UIColor orangeColor],[UIColor purpleColor], nil];
       if(changeCount < 7){
          imageColor1 = [colors objectAtIndex:changeCount];
       }
       else{
          changeCount = changeCount - 5;
          imageColor1 = [colors objectAtIndex:changeCount];
       }
       if(changeCount1 < 7){
          imageColor2 = [colors objectAtIndex:changeCount1];
       }
       else{
          changeCount1 = changeCount1 - 5;
          imageColor2 = [colors objectAtIndex:changeCount1];
       }
       changeCount++;
       changeCount1++;
    }

基本上,每当用户点击“更改颜色”按钮时,变量 changeCountchangeCount1 都会增加它们的计数,并且 self.imageColor1< 中的值self.imageColor2 更改为数组 colors 中的后续值。出于某种原因,这段代码不起作用,颜色也没有改变。 createButton 方法有效,因为每当我按下它时,都会出现一个新按钮,但是如果我创建一个按钮然后按下更改颜色按钮然后创建另一个按钮,新按钮的颜色仍然是相同的。所以基本上我需要知道我的代码有什么问题。提前致谢。

最佳答案

那是因为您每次都重置了 changeCount 和 changeCount1 变量。注意到您重新创建了它们并在开始时将它们设置为 0 和 1 吗?所以他们当然永远不会改变。您正在创建它们,就好像它们是静态变量一样,但它们不是。它们是局部变量。 NSArray 可以是静态的,但计数变量最好作为成员变量(即在您的界面中定义)。

关于objective-c - iOS 颜色数组检索错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10942061/

相关文章:

ios - 在 UIView 上设置多个边框

iPhone/iOS : Will there be called any method if a UIView is added as a subview

ios - 点击手势识别器 - 哪个对象被点击了?

ios - 来自另一个类的 Cocoa Touch 方法被调用但代码不运行

ios - NSDateFormatter dateFromString 格式字符串问题

ios - 推一个新的viewController(没有segue)给我一个黑屏?

ios - 在 Xcode 中调试时,我可以看到我的应用程序中的 View 层次结构吗?

ios - AFNetworking JSON responseObject 已修改格式

ios - 未能找到 zlib 的构建 b/c 符号

ios 委托(delegate) Objective-c 数据从第二个 View Controller 发送到第一个 View Controller