Swift - determine which button was pressed with switch

前端 未结 5 630
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-17 01:04

I have 4 buttons that call one function. Depending on which button was pressed i need to hide button inside of function that called after pressing.I dont know which button w

相关标签:
5条回答
  • 2020-12-17 01:28

    Put your code in like below as i shown:

    func btnSomeButtonClicked(sender: UIButton)
    {
    let tag = sender.tag;
           //put your same code here and either use tag variable as i shown or put as you did.
    }
    

    --> due to this you will get sender's property easily.

    -->Also check that you didn't forgot to give tags to the UIButtons.

    0 讨论(0)
  • 2020-12-17 01:29

    Try this,

    1) Assign tag for each button

    Button1.tag=1
    Button2.tag=2
    Button3.tag=3
    Button4.tag=4
    

    2) Then check your common button action

    func buttonClicked(sender: UIButton)
    {
       switch sender.tag {
       case 1: self.sender.hidden = true //button1
               break;
       case 2: self.sender.hidden = true //button2
               break;
       case 3: self.sender.hidden = true //button3
               break;
       case 4: self.sender.hidden = true //button4
               break;
       default: ()
               break;
       }
    
    }
    
    0 讨论(0)
  • 2020-12-17 01:31

    Rather then using a switch statement you can just use the sender directly.

    Note that you need to specify that the sender is a UIButton (therefore this will only work if the function is only called from a button).

    @IBAction func submitButton(sender: UIButton) {
    
        /* extra code here */
    
        sender.enabled = false
    
    }
    

    This reduces the code and also allows you to add extra buttons without code changes.

    0 讨论(0)
  • 2020-12-17 01:36

    Why you always want to use tags to determine the UIButton ? If the called function has a sender parameter then you know the UIButton which called the function through this parameter.

    func buttonClicked(sender: UIButton)
    {
       switch sender {
       case button1: // Do something
       case button2: // Do some other stuff
       ...
       default: ()
       }
    
    }
    
    0 讨论(0)
  • 2020-12-17 01:37

    I know this has been answered, but if you set this up in IB, then I would recommend you check if the button was pressed by using the @IBOutlet variables as cases.

    class SomeView: UIView {
        @IBOutlet weak var buttonA: UIButton!
        @IBOutlet weak var buttonB: UIButton!
        @IBOutlet weak var buttonC: UIButton!
    
        @IBAction func didHitButton(_ sender: UIButton) {
            switch sender {
            case buttonA:
                doThis()
            }
            case buttonB:
                doThat()
            }
            case buttonC:
                doThisAgain()
            }
            default:
                doNothing()
            }
        }
        ...
    }
    

    Now you hook up the buttons in your NIB to each @IBOutlet, and all of the buttons to the one @IBAction didHitButton.

    This is pretty straight forward, and you don't have to worry about tags or changing names.

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