开发者

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 开发者_JAVA百科anywhere!!


This isn't possible out of the box. (See How can I enable multiple segments of a UISegmentedControl to be selected?.)

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) 
@end

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);
    }
}
@end

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜