How to connect multiple buttons in a storyboard to

2019-01-11 17:36发布

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.

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?

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

7条回答
ら.Afraid
2楼-- · 2019-01-11 18:19

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

查看更多
一纸荒年 Trace。
3楼-- · 2019-01-11 18:21

You can use IBOutletCollection the following link will help you in using that

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

查看更多
男人必须洒脱
4楼-- · 2019-01-11 18:22

Swift 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.

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

Use sender.tag to identify the tapped button.

查看更多
Summer. ? 凉城
5楼-- · 2019-01-11 18:22

Follow this steps

1. Create IBAction In View Controller

In .h file

-(IBAction)action:(id)sender;

In .m File

-(IBAction)action:(id)sender{


}

2 . Connect all button to this action

enter image description here

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

4 . Now inside your action function put these

    -(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.

查看更多
Root(大扎)
6楼-- · 2019-01-11 18:22

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

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 IBAction method either by reading the text from the button or using the tag attribute which you should have set from IB.

- (IBAction)buttonClicked:(id)sender {
    NSLog(@"Button pressed: %@", [sender currentTitle]);
    NSLog(@"Button pressed: %@", [sender tag]);
}
查看更多
在下西门庆
7楼-- · 2019-01-11 18:27

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

查看更多
登录 后发表回答