IOS:一个IBAction用于多个按钮

发布于 2024-11-04 18:15:48 字数 64 浏览 1 评论 0原文

在我的项目中,我必须控制40个按钮的操作,但我不想创建40个IBAction,我可以只使用一个IBAction吗?

In my project I must control action of 40 buttons, but I don't want to create 40 IBAction, can I use only a IBAction, how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

从来不烧饼 2024-11-11 18:15:48

如果您使用界面生成器来创建按钮,只需将它们指向相关类中的相同 IBAction 即可。

然后,您可以通过从按钮读取文本来区分 IBAction 方法中的按钮......

- (IBAction)buttonClicked:(id)sender {
    NSLog(@"Button pressed: %@", [sender currentTitle]);    
}

或者通过在 Xcode 中设置 tag 属性并通过 [sender 读回它标签]。 (如果您使用此方法,请从 1 开始标记,因为 0 是默认值,因此用处不大。)

If you're using interface builder to create the buttons, simply point them at the same IBAction in the relevant class.

You can then differentiate between the buttons within the IBAction method either by reading the text from the button...

- (IBAction)buttonClicked:(id)sender {
    NSLog(@"Button pressed: %@", [sender currentTitle]);    
}

...or by setting the tag property in Xcode and reading it back via [sender tag]. (If you use this approach, start the tags at 1, as 0 is the default and hence of little use.)

十年不长 2024-11-11 18:15:48
-(IBAction)myButtonAction:(id)sender {
    if ([sender tag] == 0) {
        // do something here
    }
    if ([sender tag] == 1) {
        // Do something here
    }    
}

// in Other words

-(IBAction)myButtonAction:(id)sender {
        switch ([sender tag]) {
        case 0:
            // Do something here
            break;
        case 1:
           // Do something here
             break;
       default:
           NSLog(@"Default Message here");
            break;
}
-(IBAction)myButtonAction:(id)sender {
    if ([sender tag] == 0) {
        // do something here
    }
    if ([sender tag] == 1) {
        // Do something here
    }    
}

// in Other words

-(IBAction)myButtonAction:(id)sender {
        switch ([sender tag]) {
        case 0:
            // Do something here
            break;
        case 1:
           // Do something here
             break;
       default:
           NSLog(@"Default Message here");
            break;
}
天气好吗我好吗 2024-11-11 18:15:48

将所有按钮设置为使用该操作。操作通常有一个 sender 参数,您可以使用该参数来确定哪个按钮正在调用该操作。区分按钮之间差异的一种流行方法是为每个按钮的 tag 属性分配不同的值。因此,您可能有 40 个按钮,其标签范围从 1 到 40。(0 可能不是标签的最佳选择,因为这是默认值,并且您忘记设置标签的任何按钮都将使用 0 作为标签 )

当所有按钮都执行大致相同的操作(例如计算器或键盘上的按钮 时,此技术最有用。如果每个按钮执行完全不同的操作,那么您最终仍然会得到相当于 40 个方法的结果,但您可以用自己的 switch 语句替换 Objective-C 的消息传递系统。在这种情况下,最好花时间创建所需数量的操作并适当地分配它们。

Set all the buttons to use that one action. Actions generally have a sender parameter, which you can use to figure out which button is calling the action. One popular way to tell the difference between buttons is to assign a different value to each button's tag property. So you might have 40 buttons with tags ranging from 1 to 40. (0 probably isn't a great choice for a tag since that's what the default value is, and any button for which you forget to set the tag will have 0 as the tag value.)

This technique is most useful when all the buttons do approximately the same thing, like the buttons on a calculator or keyboard. If each of the buttons does something completely different, then you still end up with the equivalent of 40 methods, but you substitute your own switch statement for Objective-C's messaging system. In that case, it's often better just to spend the time to create as many actions as you need an assign them appropriately.

书信已泛黄 2024-11-11 18:15:48

当然。只需将所有按钮连接到 Interface Builder 中的相同操作方法即可。使用该方法的 sender 参数(可能与按钮的 tag 属性结合使用)来识别哪个按钮正在发送事件。

Sure. Just connect all buttons to the same action method in Interface Builder. Use the method's sender argument (possibly in conjunction with the buttons' tag property) to identify which button is sending the event.

娇妻 2024-11-11 18:15:48

只需使用一个 IBAction 并将其分配给您的所有按钮。

Just use one IBAction and assign it to all your buttons.

秋日私语 2024-11-11 18:15:48

我自己就使用了上述方法,选择了一些按钮,但将它们全部转换并使用 switch case 代替

-(IBAction)buttons:(id)sender
{

    switch ([sender tag])

    {

        case 0 :

    }
}

Just used the above method myself , had a selection of buttons but converted them all and used switch case instead

-(IBAction)buttons:(id)sender
{

    switch ([sender tag])

    {

        case 0 :

    }
}
我们的影子 2024-11-11 18:15:48

看起来你已经得到了你需要的所有答案,但我想添加到其他人的答案中。

您想要使用 1 个 IBAction 还是 40 个操作取决于您希望按钮执行的操作。如果所有按钮执行完全不同的操作,则需要所有单独的 IBAction,但如果希望所有按钮执行相同的操作,则可以只使用一个。我需要这些按钮和操作的更多详细信息,但您可能有每个按钮的标题,因此您可以使用它来区分每个按钮并创建消息或通过按下的特定按钮自定义的内容。这是一个例子。每次按下按钮时,标签都会显示一条消息,显示“按下按钮的标题”。

通过这种方式,您不需要对所有 40 个模式进行 switch case。您仍然可以通过按下 2-3 行代码的按钮来显示或执行个性化的操作。

- (IBAction)button_Clicked:(UIButton *)sender {

    //Get the buttons' titles.
    NSString *title =[sender titleForState:UIControlStateNormal];

    //Construct a message that includes the *title. 
    NSString *plainText=[NSString stringWithFormat:@"%@ button pressed.", title];

    //Assigns the *plainText to the label. 
    self.Label.text=plainText;

}
@end

Seems like you are getting all the answers you need, but I wanted to add to everyone else's answers.

Whether you want to use one IBAction or 40 actions is up to what you want the buttons to do. If all the buttons do completely different things, you need all separate IBActions, but if you want all of them to do the same thing, you can use just one. I need more details of those buttons and action, but you probably have title for each button, so you can use that to differentiate each button and create a message or something that's being customized by the particular button pressed. Here is the example. Each time a button is pressed, a label displays a message that say "i.e.title of the button" pressed.

By doing it this way, you don't need to do switch case with all 40 patterns. You can still display or do something that's individualized by the button pressed with just 2-3 lines of code.

- (IBAction)button_Clicked:(UIButton *)sender {

    //Get the buttons' titles.
    NSString *title =[sender titleForState:UIControlStateNormal];

    //Construct a message that includes the *title. 
    NSString *plainText=[NSString stringWithFormat:@"%@ button pressed.", title];

    //Assigns the *plainText to the label. 
    self.Label.text=plainText;

}
@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文