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

后端 未结 7 1066
梦谈多话
梦谈多话 2020-12-03 10: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 s

相关标签:
7条回答
  • 2020-12-03 10:57

    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:

    0 讨论(0)
  • 2020-12-03 10:57

    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

    0 讨论(0)
  • 2020-12-03 10:58

    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]);
    }
    
    0 讨论(0)
  • 2020-12-03 11:00

    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.

    0 讨论(0)
  • 2020-12-03 11:02

    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

    0 讨论(0)
  • 2020-12-03 11:14

    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.

    0 讨论(0)
提交回复
热议问题