【问题标题】:UIButton doing different action iosUIButton 做不同的动作 ios
【发布时间】:2013-03-21 12:16:12
【问题描述】:

我有两个导航按钮(左和右)和两个类别按钮(例如 loveCategory 和 otherCategory)。当我选择 loveCategory 时,我希望我的导航按钮仅从 loveCategory 显示(在我的情况下为图像),当我按下 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?
}
}

【问题讨论】:

  • @chirag - 拼写错误?大声笑
  • 我没有得到你的确切信息,请解释更多。 only from loveCategoryonly from otherCategory 是什么意思。您想为不同的类别使用不同的导航按钮吗?
  • 可以更详细一点。我在数据库(sqlite)中有 loveCategory 和 otherCategory 的图像。我有两个导航按钮(左和右)。按下loveCategory 后,我使用导航按钮左右移动以显示从DB 获取的图像,但是当我单击otherCategory 并使用相同的左右按钮(位于-(IBAction)NavigationBtnTapped:(UIButton *)sender),我又得到了 loveCategory 的图像,但不是 otherCategory。

标签: ios button uibutton action ibaction


【解决方案1】:

为您的按钮添加标签 1 和 2,然后试试这个:

    -(IBAction)NavigationBtnTapped:(id)sender {

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

}

【讨论】:

  • 我已经这样做了.. 我的问题是如何使用 NavigationBtnTapped 操作来执行其他操作?我实际上想在另一个 -(IBAction)loveCategory 中调用这个 NavigationBtnTapped 方法
  • 只需根据点击的按钮调用相关方法即可。有什么问题?
  • 我如何调用 -(IBAction)loveCategory { -(IBAction)NavigationBtnTapped:(id)sender // 是否可以在另一个动作中调用一个动作? }
【解决方案2】:

另一种方式是......

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

【讨论】:

    【解决方案3】:

    另一种方法是拥有 2 个 UIButtons 和两个 UIImages 但这不是一个好方法:D

    一开始loveCategory的按钮隐藏=NO,图像隐藏=YES,其他类别按钮=YES,图像隐藏=NO。

    当您单击loveCategory 的按钮时,loveCategory 的按钮隐藏=YES 并且其图像隐藏=NO 并且对于otherCategory 的按钮隐藏=NO 并且图像隐藏=YES

    【讨论】:

      【解决方案4】:

      您可以通过设置标志来确定最后选择的类别

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

      【讨论】:

      • 我想在喜欢的类别中调用 NavigationBtnTapped,如下所示 -(IBAction)loveCategory { -(IBAction)NavigationBtnTapped:(id)sender { // 是否可以在另一个动作中调用一个动作? } }
      • 它似乎不起作用..我创建了另一个名为 -(IBAction)loveNavigationBtnTapped:(UIButton*)sender 的操作并尝试在 (-IBAction)loveCategory { [self loveNavigationBtnTapped:null ]; } 并且它不工作..
      • I created another action called 是什么意思以及它是如何不工作的。
      【解决方案5】:

      找到答案了!

      在头文件中

      BOOL isLove,isOther;
      

      在实现文件中

      - (void)viewDidLoad
      {
          isOther=TRUE;
          isLove=FALSE;
      }
      
      -(IBAction)NavigationBtnTapped:(UIButton*)sender {
      if (isLove) {
      // Statement
      }
      else
      {
      //statement
      }
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多