Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While I agree that tag will work in many circumstances, it's a pain to work with integer tags when you have a matrix of 100 buttons that are pushable where you want to identify: (1) what was pushed and (2) what value was pushed. I found myself starting to invent row/column naming schemes with tag numbers which got out of hand.</p> <p>I wanted multiple controls to share the same handler, and I wanted to carefully design my UI in IB to pass name/value pairs off to a key-value store based on what was pushed.</p> <p>As an alternative to tags, I came up with another workaround for UISegmentedControl (it works for other controls too).</p> <p>(1) Create a new class and sub-class UISegmentedControl. Add a string property MyCustomKey to your new class.</p> <pre><code>@interface CustomSegmentedControl : UISegmentedControl @property (nonatomic, retain) NSString* myCustomKey; @end </code></pre> <p>(2) Back in IB, modify your UISegmentedContol instances to be instances of CustomSegmentedControl. Used the IB "User Defined Runtime Attributes" to set the value of myCustomKey using IB. KeyValueCoding will take care of the setting of the properties for you:</p> <p><img src="https://i.stack.imgur.com/TOmg3.png" alt="screen shot of setting your custom property through interface building"></p> <p>(3) Wire up all your buttons to a single IBAction.</p> <p>(4) In your IBAction, you can extract the key-value pairs now by reading back your custom attribute:</p> <pre><code>- (IBAction)myButtonPressed:(id) sender { if ([sender isKindOfClass:[CustomSegmentedControl class]] == false) return; // get the key-values from the control CustomSegmentedControl *control = (CustomSegmentedControl *)sender; NSString *key = [control valueForKey:@"myCustomKey"]; NSNumber *value = [NSNumber numberWithInteger:[control selectedSegmentIndex]]; NSLog(@"Pressed control %@ with a value of %@", key, value); } </code></pre> <p>In this case I was only interested in the integer value of a segmented control, but you could get any property off the control as the value.</p> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload