4

It seems that a common way to make a text behave like a link on iOS is to make it a UIButton, but I noticed the UIButton's addTarget or gestureRecognizer's addTarget both don't have an argument that can be passed to the method when the button is pressed?

The situation is, from a server, we may get back a list of words, such as "pineapple", "apple", "orange", and the numbers can vary. These words are displayed on screen, and pressing the word will invoke a ViewController to replace the main view controller.

It seems that one way is to use UIButton's tag, so when we set up the button, we give it a tag of 0, and in another array of the current view or view controller, make element 0 point to an NSString object, containing the word "pineapple". And so in the handler, the tag can be obtained, and it can be used to retrieve the string. But is this the only way, because it seems not very structural. Is there a better way?

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • 1
    IMO, the cleanest way is to subclass UIButton to store the data you want attached to the button. – tc. Sep 23 '12 at 03:29
  • so if UIKit doesn't have a UITextLink class, such a class can be created using this method? – nonopolarity Sep 23 '12 at 03:40
  • 1
    But you can get the currentTitle property of the button in the action method, isn't that what you want to do? – rdelmar Sep 23 '12 at 04:13
  • 1
    1. Subclassing `UIButton` actually does _not_ work well; 2. You can use associative references or other techniques to create your own `NSString` properties that you can add to the `UIButton` control; 3. looking up the tag number and or looking up the `currentTitle` in your model structures might be just as easy. – Rob Sep 23 '12 at 04:45
  • @rdelmar that's true, in this case they actually match. I wasn't thinking of using the label text because it tend to be the presentation part... if some change is made such as if the label reads "look at fruit number one", then it will break down – nonopolarity Sep 23 '12 at 08:46

2 Answers2

1

My knee-jerk answer was to simply suggest that you subclass the UIButton. Whenever you want to add a property to an existing class, "subclass" is the first answer that comes to mind. But when I tried subclassing the UIButton, it did not work well. Searching for for "UIButton subclass" I discover that this is a well known issue with several recommended solutions:

I've tried both creating a category with associative references as well as the simplistic approach of just subclassing a UIView instead, and make the desired button a subview of that. Both approaches work fine. But the intuitively attractive option of just subclassing UIButton does not work well.

But while the various work-arounds for adding properties to UIButton objects work, they seem sufficiently unintuitive that, I'd be inclined to go back to something simple, such as using the tag numbers in an array or dictionary, rather than pursuing these cumbersome button subclassing techniques.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
0

Create a mutable dictionary. To associate a word with a button, add the buttons address wrapped in a nsnumber as the key and the word as the object. When the button is pressed and sends the action message along with 'sender', you can retrieve the current word from the dictionary.

David H
  • 40,852
  • 12
  • 92
  • 138