Dropdown in Swift

Asked

Viewed 98 times

0

I’m using a kind of dropdown using this project 1, and now wanted to select one of the items that is inside it and close the dropdown and the chosen item appear on the button label this all in Swift, someone can help me here?

Thank you

1 answer

1


First you have to create an Iboutlet for the button that will have your title changed: (Don’t forget to connect it to the button on the storyboard)

@IBOutlet var botao: UIButton!

There in "func createPicker()" add the following line in the loop where you create the popup options:

button.addTarget(self, action: "clicado:", forControlEvents: .TouchUpInside)

The line above tells the button being created, which method q it should call when clicked.

Then you create the method:

func clicado(sender : UIButton){
   // acha o titulo de acordo com a tag do botão clicado
   let feeling = properties.moods[sender.tag].first!
   // atribui o titulo achado ao botão do IBOutlet criado
   self.botao.setTitle(feeling.1, forState: .Normal)
   // fecha o picker
   self.closePicker()
}
  • Ccastro, can you tell me the reason for the two dots at the end of the string in the method name in the action (clicked)?

  • Do you speak these two points? -> function clicked(Sender : Uibutton) it specifies the type q will be received by the method. In this case, the Sender will receive a Uibutton, that is, the Uibutton q has been clicked. So when I do Sender.tag below, I’m taking the Uibutton tag that was clicked, so I can identify which was the option

  • No, inside the same addTarget string... action: "clicked:"

  • I don’t know if it’s clear?

  • Yeah, he was, yeah! However it is very strange you pass a self parameter and in the second parameter be required to put two points for the first parameter to work.

  • Jadson, a fix. Looking at the documentation recently, the first parameter is not the button, but the target. That is, the class where the function to be called was defined.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.