View doesn't disappear when switching views
I am calling turnView method who must get dissapear menuView (current) and may display adjustView. When method is called, it is executed with no errors but it does not switch views, menuView keeps and it returns to following line of method call. How to solve it? Thanks
call from menuView:
- (void)tableView:(UITableView *)开发者_开发知识库tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
aboutTabController * myObject = [[aboutTabController alloc] init];
if (indexPath.section == 1) {
switch (indexPath.row) {
case 0:
[myObject turnView];
break;
case 1:
//
break;
default:
break;}
}
}
method implemented on controller, menuController was defined for menuView class:
- (void) turnView
{
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
if (self.adjustController == nil)
{
adjustView *aController = [[adjustView alloc] initWithNibName:@"adjustView" bundle:nil];
self.adjustController = aController;
[aController release];
}
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[self.menuController viewWillDisappear:YES];
[self.adjustController viewWillAppear:YES];
[self.menuController.view removeFromSuperview];
[self.view insertSubview:adjustController.view atIndex:0];
[self.menuController viewDidDisappear:YES];
[self.adjustController viewDidAppear:YES];
[UIView commitAnimations];
}
There are some major conceptual problems in your code, let me try to address them one by one:
Logic in didSelectRowAtIndexPath:
You are creating a new view controller no matter which row is being selected. You should only create it when you need it. Also you if and switch statements are very convoluted and difficult to read.
Calling delegate methods
The methods viewWillAppear:
viewDidDisappear:
etc. are there for you to catch these events, not to call them yourself. This might make sense in some cases, but they will have absolutely no effect in your animation block.
Animation with blocks, or using built-in animations
You should do your animation with the new block syntax. However, for just flipping to the next view, you can use an official API from Apple without having to program your own animation.
Thus,
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section==1 && indexPath.row==0) {
adjustView *aController = [[adjustView alloc] initWithNibName:@"adjustView" bundle:nil];
aController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:aController animated:YES];
[aController release];
}
}
You can get rid of the controller by calling [self dismissModalViewControllerAnimated:YES]
and the animation will automatically be in the other direction.
精彩评论