iPhone Development

Just another iPhone Blog

UISegmentControl + Toggle

Here is one way to give Toggle property to UISegmentedControl

@interface ToggleSegmentControl : UISegmentedControl

{

NSUInteger selectedSegment;//To Keep a Backup of Selected Segment

}

 

@end

 

 

@implementation ToggleSegmentControl

 

– (id)initWithFrame:(CGRect)frame {

if (self = [super initWithFrame:frame])

{

// Initialization code selectedSegment = self.selectedSegmentIndex;

}

return self;

}

 

-(void)awakeFromNib {

selectedSegment = self.selectedSegmentIndex;

}

 

– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

 

{

//Check if both segments are equal

//self.selectedSegmentIndex will have the updated value, where as selectedSegment will contain the old value

if( selectedSegment == self.selectedSegmentIndex && selectedSegment!=-1)

{

//if both are some, deselect the selected segment self.selectedSegmentIndex=-1; selectedSegment=-1; } else { selectedSegment=self.selectedSegmentIndex;

}

}

 

@end


Advertisements

One response to “UISegmentControl + Toggle

  1. Herbert Altink January 21, 2012 at 2:03 pm

    Do you want to get the oppertunity to deselect selected segments? You can also do this by subclassing the UISegmentedControl. http://goo.gl/guK5N

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: