0

I am creating an iOS app as defined here! So here in this app I am using a segmented control (Dynamically added) to give the user the choice to select one of the options. When the user selects a choice I want to send a string to selector method, so I wanted to know if we can send anything apart from id and event to an action:@selector that we add.

Here's the code

NSArray *optionsArray =[NSArray arrayWithArray:qna2.answers];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:optionsArray];
segmentedControl.frame = CGRectMake(xOffset,yOffset, 250, 50);
[segmentedControl setSelectedSegmentIndex:UISegmentedControlNoSegment];
[self.view addSubview:segmentedControl];
yOffset+=100;

[segmentedControl addTarget:self action:@selector(MyAction:) forControlEvents: UIControlEventValueChanged];

So can we do like @selector(Myaction: Category) ?

Community
  • 1
  • 1
Vij
  • 85
  • 11

2 Answers2

0

Your best bet is probably to store the string in a property like this:

@property (nonatomic, copy) NSString *myString;

Then, retrieve the value in the myAction: method.

Or you could subclass your control and add a property that way:

@interface MySegmentedControl : UISegmentedControl

@property (nonatomic, copy) NSString *stringProperty;

@end

When you set each segmented control:

MySegmentedControl *segmentedControl = [[MySegmentedControl alloc] initWith...];
segmentedControl.stringProperty = @"Some meaningful value";
[segmentedControl addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventValueChanged];

  In the change handler:

- (void) myAction:(MySegmentedControl *)sender
{
    NSString *importantString = sender.stringProperty;
    // do stuff with importantString
}
Zev Eisenberg
  • 8,080
  • 5
  • 38
  • 82
  • I will be adding 10s of questions each belonging to a specific category, so when a particular segmentedcontrol is touched, I want that method to know which category does that question belong to?... In this scenario How would I do it? – Vij Apr 14 '14 at 02:28
  • Thanks to @Zev, I could resolve the issue by adding a property accessibilityHint to the segmentedcontrol. – Vij Apr 14 '14 at 02:45
  • I meant store the value in a property in your class, not in the segmented control. It’s bad practice to overload existing properties like that - what if you need to use `accessibilityHint` later for its intended purpose? – Zev Eisenberg Apr 14 '14 at 13:40
  • There are many segmented controls being shown on the screen, I would appreciate if you could explain with an example how would a class property help me in determining which segmented control was activated?.. I have to remind you that I am new to iOS and objective-c ... – Vij Apr 15 '14 at 05:31
  • Oops, changed the `myString` property from `strong` to `copy`, which is best practice for properties of types that have mutable subclasses (like `NSString` and `NSMutableString`), in case the string you passed in later gets mutated. I also added an example of the subclassing code. – Zev Eisenberg Apr 15 '14 at 20:36
  • Thanks a lot, this makes sense, i have tried this and it works. – Vij Apr 16 '14 at 20:07
0

My approach would be to subclass UISegmentedControl, say MYUISegementedControl, and create a delegate protocol so that it can call back to your UIViewController.

When you create your MYUISegmentedControl instance you can add whatever additional properties you need and pass this information back to the delegate, or at the very least your delegate can access these properties off the MYUISegmentedControl properties.

You could also use a category rather than a subclass.

Paulw11
  • 108,386
  • 14
  • 159
  • 186