ios 如何将故事板中的多个按钮连接到单个动作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16050516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:12:42  来源:igfitidea点击:

How to connect multiple buttons in a storyboard to a single action?

iosobjective-cuiviewuibuttoninterface-builder

提问by jab

I'm currently using Xcode 4.6 and I'm simply wondering how to select multiple buttons across different UIViews, but under a single view controller. CMD clicking doesn't seem to work. I need this functionality because in Xcode 4.6 the only way to let two buttons on IB to share the same action is to select them at once and then drag the action to your view controller.

我目前正在使用 Xcode 4.6,我只是想知道如何在不同的 UIViews 中选择多个按钮,但在单个视图控制器下。CMD 点击似乎不起作用。我需要这个功能,因为在 Xcode 4.6 中,让 IB 上的两个按钮共享相同动作的唯一方法是一次选择它们,然后将动作拖动到您的视图控制器。

My ultimate goal is to get two different buttons on two different UIViews to match the same action using storyboards in Xcode 4.6. Is there a way to do this?

我的最终目标是使用 Xcode 4.6 中的故事板在两个不同的 UIViews 上获得两个不同的按钮以匹配相同的操作。有没有办法做到这一点?

EDIT: I'm currently using Xcode 4.6.1 without any luck, upgrading to 4.6.2.

编辑:我目前使用 Xcode 4.6.1 没有任何运气,升级到 4.6.2。

回答by rob mayoff

You can connect multiple buttons to a single action without selecting them all at once.

您可以将多个按钮连接到单个操作,而无需一次选择它们。

However, after creating the action, you need to save the source file containing the action before Xcode will let you connect another control to the action.

但是,在创建动作之后,您需要先保存包含该动作的源文件,然后 Xcode 才会让您将另一个控件连接到该动作。

I did this with Xcode 4.6.2:

我用 Xcode 4.6.2 做到了这一点:

connecting buttons to action

将按钮连接到操作

回答by San

Swift 3.0 Xcode 8.0+

斯威夫特 3.0 Xcode 8.0+

Connect all buttons to below method signature. Make sure argument sender is of type UIButton (Or subclass of UIButton)in-order to connect rest of the buttons from storyboard.

将所有按钮连接到下面的方法签名。确保参数 sender 的类型为UIButton(或 UIButton 的子类),以便连接故事板中的其余按钮。

@IBAction func dialconfigTapped(_ sender: UIButton) {}

Use sender.tagto identify the tapped button.

使用sender.tag来识别点击的按钮。

回答by Tunvir Rahman Tusher

Follow this steps

按照以下步骤操作

1. Create IBAction In View Controller

1.在View Controller中创建IBAction

In .h file

在 .h 文件中

-(IBAction)action:(id)sender;

In .m File

在 .m 文件中

-(IBAction)action:(id)sender{


}

2 . Connect all button to this action

2 . 将所有按钮连接到此操作

enter image description here

在此处输入图片说明

3 . Give Each Button a tag by follow the next Pictureenter image description here

3 . 按照下一张图片给每个按钮一个标签在此处输入图片说明

4 . Now inside your action function put these

4 . 现在在你的动作函数里面放这些

    -(IBAction)action:(id)sender{

        UIButton *button=(UIButton*)sender;

        if(button.tag==1){

            //your stuff!
        }
        if(button.tag==2){

            //your stuff!
        }
        if(button.tag==3){

            //your stuff!
        }

    }

Run and Go.

跑去。

回答by Srikar Appalaraju

You should be able to do this in IB. In IB, simply point them at the same IBActionin the relevant class.

你应该能够在 IB 中做到这一点。在 IB 中,只需IBAction在相关类中将它们指向相同的位置即可。

When the action comes, in case you want to know from which button you got the action, you can then differentiate between the buttons within the IBActionmethod either by reading the text from the button or using the tagattribute which you should have set from IB.

当动作出现时,如果您想知道从哪个按钮获得动作,您可以IBAction通过从按钮读取文本或使用tag您应该从 IB 设置的属性来区分方法中的按钮。

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

回答by Vinodh

You can use IBOutletCollectionthe following link will help you in using that

您可以使用IBOutletCollection以下链接来帮助您使用它

http://useyourloaf.com/blog/2011/03/28/interface-builder-outlet-collections.html

http://useyourloaf.com/blog/2011/03/28/interface-builder-outlet-collections.html

回答by onmyway133

In Xcode 5, after setting action for the 1st button, I have to build the app. This then allows me to connect other buttons action to this same IBAction method

在 Xcode 5 中,在为第一个按钮设置操作后,我必须构建应用程序。这允许我将其他按钮操作连接到同一个 IBAction 方法

回答by anyone

in xcode 4.6.3 you no more need to save before assigning action.

在 xcode 4.6.3 中,您不再需要在分配操作之前进行保存。