views:

209

answers:

1

I am trying to work with a segmented control inside a a tableview, then when a user selects an item, I'd like to show a spinner, while some info posts to a webservice.

The problem that I am having is: How do I add a delegate and access the referenced segmented Control, so I can set it's alpha or visibility to NO? Also, what's the best practice for this, I know there's tags, but not sure how they work in this type of situation.

NSArray * segmentItems= [NSArray arrayWithObjects: @"one", @"two", @"three", @"four", @"five", nil];
UISegmentedControl *segmentedControl= [[[UISegmentedControl alloc] initWithItems: segmentItems] retain];
segmentedControl.segmentedControlStyle= UISegmentedControlStyleBar;
segmentedControl.selectedSegmentIndex= -1;

[segmentedControl addTarget: self action: @selector(onSegmentedControlChanged:) forControlEvents: UIControlEventValueChanged];
segmentedControl.frame  = CGRectMake(2, 0, 300, 30);
segmentedControl.tintColor= [UIColor grayColor];

Here's my delegate

 - (IBAction)onSegmentedControlChanged:(id)sender
{
    int clickedSegment= [sender selectedSegment];
}

How do I access the UISegmentedControl from the sender so I can set the visibility Off? I can always set the my object that populates my segmentedControl by extending it, I Just need to figure out how to get a reference to the the cell and the SegmentedControl?

A: 

When you create/return the cell with the segmented control, set the delegate object before returning the cell to the table.

Edit:

Sorry, misread the question. The sender passed to the delegate methods will be the actual UISegmentedControl instance you need to identify. Inside the delegate method, cast the generic sender to a UISegmentedControl` then set its attributes as needed.

The selected table row is passed to the tableview selection methods.

TechZen
Casting is done like this, i found: UISegmentedControl *control = (UISegmentedControl *)sender;
ReduxDJ