How to connect multiple buttons in a storyboard to

2019-01-11 18:16发布

问题:

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.

回答1:

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.

I did this with Xcode 4.6.2:



回答2:

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.



回答3:

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

3 . Give Each Button a tag by follow the next Picture

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.



回答4:

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]);
}


回答5:

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



回答6:

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



回答7:

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