使用NSArray中的项声明全局变量?

时间:2014-07-01 23:42:22

标签: ios nsarray global-variables

我想实现一个功能,当我点击" operatorButton" titleColor发生了变化,但当我点击其他按钮时除了#34; operatorButton"," operatorButton" titleColor backto originalColor。

我遇到了一个问题: " operatorButton"我在" viewDidLoad"方法是局部变量,我无法访问其他方法。 如何将" operatorButton"声明为表示NSArray" operationButton"?

中每个项目的全局变量
#import <UIKit/UIKit.h>

@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *operationButton;    


- (void)viewDidLoad {

[super viewDidLoad];
for (UIButton *operatorButton  in  self.operationButton){
    [operatorButton setTitleColor:[UIColor colorWithRed:248.0/255 green:148.0/255 blue:52.0/255 alpha:1.0]
                         forState:UIControlStateSelected];
    [operatorButton setTitleColor: [UIColor colorWithRed:95.0/255 green:105.0/255 blue:114.0/255 alpha:1.0]
                         forState:UIControlStateNormal];
}

}

1 个答案:

答案 0 :(得分:1)

您已将其声明为带有IBOutletCollection的数组。您可以在该文件中的任何位置访问self.operationButton,因为它是一个属性。

为所有按钮创建一个IBAction,然后尝试以下操作:

-(IBAction)selectedButton:(id)sender
{
  UIButton *senderButton = (UIButton *)sender;
  for (UIButton *operatorButton in self.operationButton)
  {
    if ([operatorButton isEqualTo:senderButton])
    {
      //set title color to your selected Color
    }
    else
    {
      //set title color to original color on buttons not clicked
    }
  }
}