【问题标题】:Linking multiple buttons to one method with different jobs将多个按钮链接到具有不同作业的一种方法
【发布时间】:2012-04-20 05:29:23
【问题描述】:

我的故事板中有一个巨大的疯狂场景,它有 36 个不同的按钮,每个按钮在点击时都有不同的含义。我真的不想创建 36 种不同的方法,所以我如何在按下 36 个按钮之一时调用的方法中引用按钮标题或按钮名称。

这可能是一个简单的问题,但我是 iOS 和 Objective C 的新手...

谢谢!

【问题讨论】:

标签: iphone objective-c cocoa-touch methods


【解决方案1】:

您可以创建一个方法,如下所示:

- (IBAction)buttonTapped:(id)sender{

  // The button that was tapped is called "sender"
  // This will log out the title of the button

  //NSLog(@"Button: %@", sender.titleLabel.text);

  //Edit: You need a cast in the above line of code:

  NSLog(@"Button: %@", ((UIButton *)sender).titleLabel.text);
}

然后,您可以使用 Interface Builder 连接到所有按钮。您可以使用某种 if/else 逻辑来测试点击了哪个按钮。

您可以检查 titleLabel 属性,或者您可以为每个按钮分配一个 IBOutlet 并检查它。

例如:

if([sender isEqual:buttonOutlet1]){
  //If this button is attached to buttonOutlet1
  //do something
}

或者,您可以简单地使用每个按钮的标签,而不用担心插座。

第三种选择是在代码中生成和布局按钮,然后将它们作为按钮数组的元素进行访问。

第四个选项是给按钮添加标签并在你的函数中检查按钮的标签。

【讨论】:

  • 这行得通,但是我必须为每个按钮创建插座。
  • @JoeBurnett - 或者您可以使用 titleLabel 或使用数组。
  • 当我这样做时... NSLog(@"Button: %@", sender.titleLabel.text); --- 我收到此错误...在“__strong id”类型的对象上找不到属性“titleLabel”
  • @JoeBurnett 您需要将发送者转换为 UIButton,因为它对 id 类型的方法可见。查看我的编辑。
  • 在上一个示例中进行指针比较会不会更有效,因为您想知道它是否是完全相同的对象,例如指向同一个地址
【解决方案2】:

给每个按钮一个唯一的标签值。在 IBAction 中,sender.tag 告诉您点击了哪个按钮。

【讨论】:

  • hmm... 尝试 (IBAction)the_action:(UIButton*)sender... 然后 [sender tag]
  • tag 是继承自 UIView 的一个属性,它是一个 int。这将起作用。在 IB 或 button.tag = 501 代码中设置 UIButton 的标签值;将 UIButton 的操作设置为您的处理程序。将参数类型更改为 UIButton* 所以-(IBAction)the_handler:(UIButton*)sender。现在,int x = [sender tag]; 应该可以工作了。
  • 使用标签比使用标题好。标题可能会随着 GUI 更新或本地化而改变。
【解决方案3】:

您为处理按钮按下而设置的 IBAction 例程具有 sender 参数。检查以决定。

【讨论】:

    【解决方案4】:
    -(IBAction) buttonPress: (id) sender {
        UIButton *pressedButton = (UIButton *)sender;
        NSString *buttonTitle = [pressedButton currentTitle];
        if ([buttonTitle isEqualToString: @"SomeTitle"]) {
            //do work for that button.
        }
    }
    

    您可以使用各种 NSString 方法来比较或过滤哪个按钮被按下并通过 if 或开关进行处理。

    【讨论】:

      【解决方案5】:

      这很简单,但由于您是新手,这里有一个答案。
      (根据斯坦福 cs193p 课程,2010-2011 秋季(这就是他们对计算器应用程序所做的))制作一个接收参数的方法,即 UIButton。 例如:

      - (IBAction) someMethodThatDoesSomething:(UIButton *)sender;
      

      然后根据sender.titleLabel.text做if语句
      我不知道是否还有其他解决方案。希望这会有所帮助!

      【讨论】:

        【解决方案6】:

        -(IBAction)myButtonAction:(id)sender {

            if ([sender tag] == 0) {
                // do something here
            }
            if ([sender tag] == 1) {
                // Do some think here
           }
        
        }
        

        //换句话说

        -(IBAction)myButtonAction:(id)sender {

             NSLog(@"Button Tag is : %i",[sender tag]);
        
            switch ([sender tag]) {
            case 0:
                // Do some think here
                break;
            case 1:
               // Do some think here
                 break;
           default:
               NSLog(@"Default Message here");
                break;
        

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-01-26
          • 1970-01-01
          • 1970-01-01
          • 2021-03-15
          • 2012-05-02
          • 1970-01-01
          • 2011-07-19
          • 1970-01-01
          相关资源
          最近更新 更多