UISegmentControl switch views?
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:N开发者_如何转开发O];
[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
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;
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.
精彩评论