views:

331

answers:

2

Hello all, 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

A: 

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;
gerry3
These are not subviews. Is there a way to say List1Controller for the first to appear and List2Controller to appear for second?
Tanner
All views except the window are subviews. I don't understand this new question. Even if the views have view controllers, your code will still work. Make sure to use the "view" property of the view controller (and add them as subviews to a currently visible view/window).
gerry3
A: 

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.

Tom Harrington