Question

I have a UISegmentControl in my app and im trying to make it switch views like the app store. Ive tried this code with no luck:

- (IBAction)segmentSwitch:(id)sender {
  UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
  NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;

  if (selectedSegment == 0) {
    //toggle the correct view to be visible
    [firstView setHidden:NO];
    [secondView setHidden:YES];
  }
  else{
    //toggle the correct view to be visible
    [firstView setHidden:YES];
    [secondView setHidden:NO];
  }
}

Does anybody know how I could switch views? Any help is appreciated. Thanks

Was it helpful?

Solution

That code will work as long as both views are currently subviews of a visible parent view (or window).

Also, you can simplify your IBAction a bit like this:

- (IBAction)segmentSwitch:(UISegmentedControl*)segmentedControl {
    //UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
    NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;

OTHER TIPS

It would help to know what does happen when this code is executed and what the starting point is-- is firstView already visible, and is there any effect at all when the code runs?

If firstView is visible but the code never hides it, I suspect that the "firstView" variable is not actually connected to the view. You're telling firstView to hide, so if the view never hides, "firstView" is probably nil. Set a breakpoint in this method and check both firstView and secondView to make sure they have references to the views you want to manipulate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top