如何访问访问功能中的多个按钮? [英] How to access multiple buttons in access function?

查看:88
本文介绍了如何访问访问功能中的多个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在表格视图的每一行中都有两个按钮.一个被标记为拥有",另一个被标记为想要".当应用启动时,每个按钮以20%的不透明度关闭.轻按一个按钮时,不透明度设置为100%.我需要逻辑,以便如果一个按钮设置为100%不透明度,而另一个按钮设置为20%,则第一个按钮需要设置为20%,第二个按钮设置为100%(因此,需要反转不透明度).

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).

每个按钮都有其自己的动作,按下它们即可运行.我可以访问按下的按钮,并使用(UIButton * senderButton =(UIButton *)sender)设置不透明度.但是,我还需要设置其他按钮的不透明度.当按下一个按钮时,如何访问我的动作/功能内部的另一个按钮(未按下的按钮)?谢谢!

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!

推荐答案

如果我正确理解了您的问题,则可以在头文件中声明按钮,如下所示:

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

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

在m文件中的tmen(在viewDidLoad中)中,您可以使用一个选择器和不同的标签来设置此按钮:(有关创建按钮的更多信息:

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];

 }

用不同的方式声明b2:

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;
       }
 }

这篇关于如何访问访问功能中的多个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆