Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何选择IBOutletCollection内部的所有_Ios_Objective C - Fatal编程技术网

Ios 如何选择IBOutletCollection内部的所有

Ios 如何选择IBOutletCollection内部的所有,ios,objective-c,Ios,Objective C,我为button1到button9创建了一个IBMoutletCollection属性和操作。每按下一个按钮,颜色都会改变 -(IBAction)btnCollectionAction:(id)sender { counter = 0; btnSelecter = sender; if (counter == 0) { [btnSelecter setTitleColor:[UIColor whiteColor] forState:UIControlStateNo

我为button1到button9创建了一个IBMoutletCollection属性和操作。每按下一个按钮,颜色都会改变

-(IBAction)btnCollectionAction:(id)sender {
   counter = 0;
   btnSelecter = sender;

   if (counter == 0) {
      [btnSelecter setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
      counter = 1;
   }
   else  {
      [btnSelecter setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
      counter = 0;
   }

}
用于selectall按钮的IBOutlet

- (IBAction)selectButtonFunction1:(id)sender {

     if (counter == 0) {
       [selBtn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

       counter = 1;
     }
     else  {
       [selBtn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

       counter = 0;
     }

} 
上面的两个脚本都工作得很好

我的问题是-如果我按下全选按钮(更改为白色),上述九个按钮必须更改为白色或黑色

我试着这样做:

if(selBtn1.touchInside) {
    [btnSelecter setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
else {
    [btnSelecter setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}

它将只更改一个按钮而不是全部按钮。

您问题的最后一部分很模糊,我不理解最后一部分。从您的标题中,我了解到您需要从
IBOutletCollection
获取所有
ui按钮。我回答这个问题

您可以切换颜色,如下所示:

- (IBAction)selectAll:(id)sender
 {
    for (UIButton *button in buttonCollection)
    {
       if([button titleColorForState:UIControlStateNormal] isEqual:[UIColor blackColor]])
       {
          [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
       }
       else
       {
          [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
       }
    }
 }
尝试以下方法: 1.使用所有按钮创建
IBOutletCollection
(您已经准备好了)

二,。在整个集合中迭代以配置每个按钮的目标和外观:

   for (UIButton *button in self.myButtons)
    {
     [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
     [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];  
    // Do any additional configuration here
    }
三,。将目标添加到selectAll按钮,并为selectAll按钮分配标记属性

[selecAllButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
selectAllButton.tag = 10000; // you can other tag number 
四,。创建
按钮按下:
处理程序

-(void) buttonPressed:(UIButton *)sender
 {
   if(sender.tag = 10000) 
   {
     for (UIButton *button in self.menuButtons) {
       if([button isKindOfClass:[UIButton class]]){
           button.selected = YES;
        }
      }
    } else 
    {
      // handle other button actions  
    } 
 }

@香卡尔:你到底想要什么?是否要将所有按钮颜色更改为白色或黑色?或者,是否要将所选按钮的颜色更改为白色,将所有其他按钮的颜色更改为黑色?我想在按下按钮时切换该颜色我想获取所按下按钮的标题,并将其显示在另一个按钮标题上。这是可能的@Shankar:是的,如果可能的话,您可以使用sender参数。只需调试并检查sender参数的内容
-(void) buttonPressed:(UIButton *)sender
 {
   if(sender.tag = 10000) 
   {
     for (UIButton *button in self.menuButtons) {
       if([button isKindOfClass:[UIButton class]]){
           button.selected = YES;
        }
      }
    } else 
    {
      // handle other button actions  
    } 
 }