繁体   English   中英

UIButton做不同的动作iOS

[英]UIButton doing different action ios

我有两个导航按钮(左右)和两个类别按钮(例如loveCategory和otherCategory)。 当我选择loveCategory时,我希望导航按钮仅从loveCategory显示(在我的情况下为Images),当我按otherCategory时,导航按钮应该只能从otherCategory显示(图像)(即当我按下向左和向右按钮时) )。

让我知道是否不清楚。

这是我的代码:

-(IBAction)NavigationBtnTapped:(UIButton*)sender {
UIButton *button=sender;
switch (button.tag) {
    case 1:
        //statements
        break;
    case 2:
        //statements
        break;
    default:
        break;
}

}

-(IBAction)loveCategory {
}
-(IBAction)otherCategory {
}

如何在另一个动作中调用一个动作

-(IBAction)loveCategory 
{ 
  -(IBAction)NavigationBtnTapped:(id)sender
{
// Is it possible to call an action within another action?
}
}

将标签1和2赋予按钮,然后尝试以下操作:

    -(IBAction)NavigationBtnTapped:(id)sender {

    switch ([sender tag]) {
        case 1:
            //statements
            break;
        case 2:
            //statements
            break;
        default:
            break;
    }

}

另一种方式是....

给出两个UIButton 标签

button1.tag = 101;
button2.tag = 102;

两个UIButton都具有相同的方法调用。
像这样

-(void)buttonTapped:(UIButton *)sender
{
   if(sender.tag == 101)
   {
     // code for loveCategory
   }
   if(sender.tag == 102)
   {
     // code for otherCategory
   }
}

您可以通过设置标记最后选择哪个类别来做到这一点

int selectedCategory = 0;

-(IBAction)NavigationBtnTapped:(UIButton*)sender {
UIButton *button=sender;
switch (button.tag) {
    case 1:
        //statements
        if(selectedCategory == 0){
           // go left for lovecategory 
        }else{
          // go left for OtherCategory 
        }
        break;
    case 2:
        //statements
       if(selectedCategory == 0){
            // go right for lovecategory 
       }else{
           // go right for OtherCategory 
       }
        break;
    default:
        break;
}

}

-(IBAction)loveCategory {
  selectedCategory = 0;
}
-(IBAction)otherCategory {
   selectedCategory=1;
}

那是你要的吗 ?

用于在另一个动作中调用动作

-(IBAction)loveCategory 
{ 
  [self NavigationBtnTapped:null];
}
-(IBAction)NavigationBtnTapped:(id)sender
{
// Is it possible to call an action within another action?
}

另一种方法是拥有2个UIButton和两个UIImage,但这不是一个好方法:D

在一开始,loveCategory的按钮=否,它的图像隐藏=是,otherCategory的按钮= YES,它的图像隐藏= NO。

当您单击loveCategory的按钮时,loveCategory的按钮已隐藏= YES,其图像已隐藏=否,其他类别的按钮已隐藏= NO且图像已隐藏= YES

找出答案!

在头文件中

BOOL isLove,isOther;

在实施文件中

- (void)viewDidLoad
{
    isOther=TRUE;
    isLove=FALSE;
}

-(IBAction)NavigationBtnTapped:(UIButton*)sender {
if (isLove) {
// Statement
}
else
{
//statement
}

如有必要,请使用“ else if”检查多个按钮。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM