0

I have two buttons in each row of a tableview. One is labeled "have it" the other "want it" Each button starts off at 20% opacity when the app starts. When one button is tapped the opacity is set to 100% . I need logic so that if one button is set to 100% opacity and the other one set at 20% is tapped, the first button needs to be set to 20% and the second button to 100% (so the opacity needs to be reversed).

Each button has it's own action that is run when pressed. I can access the button that is pressed and set the opacity with (UIButton *senderButton = (UIButton *)sender). However I need to set the opacity of the other button as well. How can access the other button (the one that was not pressed) inside of my action/function that is called when one is pressed? Thanks!

striff88
  • 97
  • 2
  • 10

2 Answers2

0

You can create an outlet for each button. So that you can set its property from any where within its container class.

user523234
  • 14,323
  • 10
  • 62
  • 102
0

if I correct understand your question, you can declare your buttons in header-file like this:

@interface myController : UIViewController
{
  UIButton *b1;
  UIButton *b2;
}

tmen in m-file (in viewDidLoad) you can set this buttons with one selector and different tags: (for more information about creation buttons: How do I create a basic UIButton programmatically?)

-(void)viewDidLoad
 {
     [super viewDidLoad];

     b1 = [UIButton buttonwithType:UIButtonTypeCustom];
     [b1 addTarget:self withAction:@selector(clickINMyButtons:) forState:UIControlTouchUPInside];  // sorry, I don't remember correct syntax, i'll correct this some later if you needed in it.
     b1.tag = 1;  
     b1.frame = CGRectMake(0,0,12,12); //example
     [self.view addSubView:b1];

 }

alike declare b2 with different:

b2.tag = 2;

So, then you implement your selector with changing opacity:

 -(void)clickINMyButtons:(UIButton *)sender
 {
      if (sender.tag == 1)
       {
         sender.alpha = 1; // or b1.alpha = 1;
         b2.alpha = 0.2;
       }
       else if (sender.tag == 2)
       {
         sender.alpha = 1; // or b2.alpha = 1;
         b1.alpha = 0.2;
       }
 }
Community
  • 1
  • 1
frankWhite
  • 1,523
  • 15
  • 21