Retrieving the name of a boot on Swift

Asked

Viewed 168 times

2

How do I recover the value of a button ?

For example:

I have a button in my layout with the name boot, but I want to assign the name of that button to be clicked on a variable!

That is possible?

Thank you![inserir a descrição da imagem aqui]1

  • You must access the property sender.tag

  • Then Sender.tag I have access to tag here what I did, I made an array with which I needed and when the tag matches the array I display =D Thanks!

  • I’ll create an answer, you could mark it as correct?

  • Switch Sender: Anyobject to Uibutton and a print on Sender.currentTitle!

  • @Brunobafilli only one detail the W comes before the X in the alphabet and also ta missing the R in your Keyboard

2 answers

2

This will require checking the property sender.tag and use the button to identify which button is being fired.

  • It should also change the type Sender AnyObject for UIButton

1


Just you take the sender.titleLabel.text, how he is a UIButton there is no property value

How your Sender is AnyObject, you will need to cast him

let value = sender.titleLabel.text as! String

If using this button only method, I recommend changing from AnyObject for UIButton

Edit You need to grab the button label (titleLabel) and then pick up the text (text) as the above example

  • Do not recommend! String. just add ! the text property

  • For what reason Leo? as his object is AnyObject own XCode will force him to put it because he won’t recognize the property type

  • The type of the text propriedate is String? Already Sender being Anyobject you must make the conditional cast Select? Uibutton

  • But in this case, using as! String it is ensuring that it will always be text, in the context of its application that is true

  • But the best is to change the Nder to Uibutton

  • I fully agree :)

  • You’re forcing unwrap as! String. Direct force . text!

Show 2 more comments

Browser other questions tagged

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