Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

ios - Swift - determine which button was pressed with switch

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 was pressed so i tried to assign tags for each of 4 buttons to identify them by tag and use switch

I tried this

            switch sender.tag {
            case 1:
                self.button1.hidden = true
            case 2:
                self.button2.hidden = true
            case 3:
                self.button3.hidden = true
            case 4:
                self.button4.hidden = true

            }

but this doesnt work compiler says about use of unresolved identifier 'sender'

How to do this correctly ?

EDIT:

Whole function

  if self.allowMistakeVar {

        let mistakeAlert = UIAlertController(title: "Here is title!", message: "message of alert", preferredStyle: .Alert)
        let okay = UIAlertAction(title: "OK", style: .Cancel) { UIAlertAction in

            self.allowMistakeVar = false
            self.allowMistakeButton.enabled = false


            switch sender.tag {
            case answer1Text:
                self.answer1Text.hidden = true
            case answer2Text:
                self.answer2Text.hidden = true
            case answer3Text:
                self.answer3Text.hidden = true
            case answer4Text:
                self.answer4Text.hidden = true

            }

        }

        mistakeAlert.addAction(okay)
        self.presentViewController(mistakeAlert, animated: true, completion: nil )

    } else {

another case.. }

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...