Thursday, May 5, 2011

UISegmentControl problem

i have a UISegmentControl which have 2 segment in my view,the problem is whatever segment i have choosed, it will return 1 in my selectedSegmentIndex property since i want it remain at the first view when select segment 0,the event i used in my segmentcontrol is valueChanged, and the function will be call as show below: -(void)segmentSelect:(id)sender{

NSInteger select=[sender selectedSegmentIndex];

if(select==0){

 NSLog(@"this is 0");

}
else{
 NSLog(@"this is 1");
 infoViewController *viewController = [[infoViewController alloc]initWithNibName:@"infoView" bundle:[NSBundle mainBundle]];
 [[self navigationController] pushViewController:viewController animated:YES];
 [viewController release];
}

} can anybody tell what goes wrong?

From stackoverflow
  • try something like this:

     -(void)segmentSelect:(id)sender{
        UISegmentedControl* segmentedControl = sender;
    
        switch ([segmentedControl selectedSegmentIndex]) {
                case 0:
              NSLog(@"this is 0");
              break;
             case 1:
              NSLog(@"this is 1");
                                //push the controller
              break;
                default:
              NSLog(@"this is unexpected");
        } 
    }
    
    Benjamin Ortuzar : Can u add to the question the code that contains the Segmented Control?, maybe we are looking in the wrong place.

0 comments:

Post a Comment