maintaining selection in UISegmentedControl

Can i maintain the selected state of UISegmentViewControl segments?? i.e can keep a segment appear selected even if the user selects another segment?? I dont seem to find anything that does this anywhere!!

-------------Problems Reply------------

This isn't possible out of the box. (See

You could try something like this code to provide similar functionality.

I found a way arround this.I placed dark colored image behind each segment and set their hidden property to true.Then i decresed the alpha value of the uisegmented control.Then in the code when a segment is clicked i turn the visibility on or off accordingly,so multiple segments appear selected

Another solution might be using a category:

#import <UIKit/UISegmentedControl.h>
@interface UISegmentedControl (MultiSelect)

Doing this, you have in principle access to private member variables of UISegmentedControl . In particular, you have access to the array holding the button segments, which you can manipulate according to your needs by overriding setSelectedSegmentIndex:selectedSegmentIndex: .However, for various reasons, the attributes declared as private still shouldn't be accessed directly, see this link. As also suggested there, you can rather abuse KVC. So the implementation could look as follows:

@implementation UISegmentedControl (MultiSelect)
- (void)setSelectedSegmentIndex:(NSInteger)selectedSegmentIndex {
NSMutableArray *pArraySegments = [self valueForKey:@"segments"];
if ((pArraySegments) && (selectedSegmentIndex >= 0) && (selectedSegmentIndex < [pArraySegments count])) {
UIButton *pSegment = (UIButton*)[pArraySegments objectAtIndex:selectedSegmentIndex];
pSegment.selected ? (pSegment.selected = NO) : (pSegment.selected = YES);

This works for me. However, since I now read this discussion, I'm not quite sure if this is really a valid approach.

Category:iphone Views:0 Time:2010-08-12

Related post

Copyright (C), All Rights Reserved.

processed in 0.150 (s). 11 q(s)