How to check whether the UISegmentedControl is selected or not by user?
I got an 开发者_StackOverflowapp in which I allocate 5 UISegmentedControl
dynamically into the view. And got a Done button at the end. My condition that to proceed into next step (when done button is pressed), all the UISegmentControl
s "should be selected by user".
The default selection in segmentcontrol is none.
How to check whether all the UISegmentedControls
in my view is selected by the user before action on the done button is executed?
Right from the apple document, this should answer your question:
@property(nonatomic) NSInteger selectedSegmentIndex
Discussion
The default value is UISegmentedControlNoSegment
(no segment selected) until the user touches a segment.
Hope you can use that to check whether the value is user selected or not, to prevent going to next page.
NSLog(@"%i", self.segment.selectedSegmentIndex);
this results -1
if no segment is selected.
add target and action to your segmentControls for UIControlEventValueChanged. From the selector you gave in action, check which segmentControl was changed, and set it's corresponding flag (ex: array of string which are @"0" for not selected and @"1" once selected).
At any time check which flags are not set, the corresponding segmentControls were never selected.
This is how u create an UISegmentedControl
NSArray *itemArray = [NSArray arrayWithObjects: @"Title1", @"Title2", @"Title3", @"Title4",nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(0, 0, 310, 35);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:@selector(pickOne:) forControlEvents:UIControlEventValueChanged];
segmentedControl.tintColor=[UIColor grayColor];
Then to find which segment was selected,
NSString *category =[segmentedControl titleForSegmentAtIndex: [segmentedControl selectedSegmentIndex]];
if(category==@"Title1"){
//Do something here..
}
Hope this helps.... Happy Coding
精彩评论