这是 rootviewcontrollerdelegate 定义
@protocol RootViewControllerViewDelegate
-(void)toggleView:(UIViewController )newController viewController:(UIViewController )oldController;
@结尾
toggleView 的可能实现
-(void)toggleView:(UIViewController *)newController viewController:(UIViewController*)oldController {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:([oldController.view superview] ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromLeft) forView:self.view cache:YES];
[newController viewWillAppear:YES];
[oldController viewWillDisappear:YES];
[oldController.view removeFromSuperview];
[self.view addSubview:newController.view];
[oldController viewDidDisappear:YES];
[newController viewDidAppear:YES];
[UIView commitAnimations];
[oldController release];
}
这将通过翻转视图来滑动视图控制器
显然,您必须在某处创建一个新的 RootViewController 并从那里的视图开始,(可能是应用程序委托)
现在,如果您希望 ViewController 能够使用 RootViewController 它必须符合协议,您可以在该类接口中声明它,如下所示
@interface MyViewController : UIViewController <RootViewControllerDelegate> {
id delegate;
}@property(assign) id <RootViewControllerViewDelegate> delegate;
现在您可以使用 delegates 方法将视图交换为另一个视图,因为一切都已正确初始化。交换两个控制器视图的代码可能如下所示
NewViewController *viewController=...
//you can set up your viewControllers data here if you need to
//Since its probable that this view has that data it can just set it instead of
//delegating
viewController.delegate=delegate; //setting up the RootViewController reference
[delegate toggleView:viewController viewController:self];
remember on the toggleView call back to release the old ViewController, if you dont youll get a leak since you lose all reference to that controller.