我有UIViewControllers
A和B,他们被分配AppDelegate
。 我需要应用过渡到它们。 如何将它们转运不重新分配和更换UIViews
? 此代码从我的电话UIBarButtonItem
中UINavigationController
:
[UIView transitionFromView:self.view //UIViewController A
toView:appDelegate.secondViewController.view //UIViewController B
duration:0.5
options:UIViewAnimationOptionTransitionFlipFromLeft
此方法替换UIViews
在我UIViewControllers
,我可以转换他们回来,或者只是不知道该怎么做。 你能告诉我如何做到这一点?
如果你在iOS 5中的世界,并希望各视图控制器之间跳转,你可能想追求视图控制器遏制 。 入住或退房手续WWDC 2011届102 。
视图控制器遏制基本假设你有被管理多个子控制器之间的导航一些父视图控制器。 在你的情况下,父视图是一个与它的按钮,导航栏。
更新:
如果你追求围堵,您可以创建具有其上的按钮,导航栏,父视图控制器。 当装载视图,您可以添加的第一个孩子视图。 因此viewDidLoad
可能看起来像:
- (void)viewDidLoad
{
[super viewDidLoad];
// this is my model, where I store data used by my view controllers
_model = [[MyModel alloc] init];
// let's create our first view controller
OneViewController *controller = [[OneViewController alloc] initWithNibName:@"OneViewController" bundle:nil];
// pass it our model (obviously, `model` is a property that I've set up in my child controllers)
controller.model = _model;
// let's put the new child in our container and add it to the view
[self addChildViewController:controller];
[self configureChild:controller];
[self.view addSubview:controller.view];
[controller didMoveToParentViewController:self];
// update our navigation bar title and the label of the button accordingly
[self updateTitles:controller];
}
该configureChild
只是做最后的配置。 作为方便起见,我经常会说我在IB建立一个UIView(在这种情况下,所谓的childView
),我使用了设置的框架,这使我摆脱了手动创建帧的世界,但你可以做任何你想要的方式:
- (void)configureChild:(UIViewController *)controller
{
// configure it to be the right size (I create a childView in IB that is convenient for setting the size of the views of our child view controllers)
controller.view.frame = self.childView.frame;
}
这是操作,如果你在导航栏中点击按钮。 如果你在第一个控制器的时候,成立了第二个控制器。 如果在第二个控制器的时候,成立了第一个:
- (IBAction)barButtonTouchUpInside:(id)sender
{
UIViewController *currentChildController = [self.childViewControllers objectAtIndex:0];
if ([currentChildController isKindOfClass:[OneViewController class]])
{
TwoViewController *newChildController = [[TwoViewController alloc] initWithNibName:@"TwoViewController" bundle:nil];
newChildController.model = _model;
[self transitionFrom:currentChildController To:newChildController];
}
else if ([currentChildController isKindOfClass:[TwoViewController class]])
{
OneViewController *newChildController = [[OneViewController alloc] initWithNibName:@"OneViewController" bundle:nil];
newChildController.model = _model;
[self transitionFrom:currentChildController To:newChildController];
}
else
NSAssert(FALSE, @"Unknown controller type");
}
这确实基本过渡(包括各种遏制相关的调用):
- (void)transitionFrom:(UIViewController *)oldController To:(UIViewController *)newController
{
[self addChildViewController:newController];
[self configureChild:newController];
[self transitionFromViewController:oldController
toViewController:newController
duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self updateTitles:newController];
}
completion:^(BOOL finished){
[oldController willMoveToParentViewController:nil];
[oldController removeFromParentViewController];
[newController didMoveToParentViewController:self];
}];
}
这种方法只是建立在基于选择哪个孩子在我们的父视图控制器的导航栏标题。 它还设置了按钮以引用其他控制器。
- (void)updateTitles:(UIViewController *)controller
{
if ([controller isKindOfClass:[OneViewController class]])
{
self.navigationItemTitle.title = @"First View Controller"; // current title
self.barButton.title = @"Two"; // title of button to take me to next controller
}
else if ([controller isKindOfClass:[TwoViewController class]])
{
self.navigationItemTitle.title = @"Second View Controller"; // current title
self.barButton.title = @"One"; // title of button to take me to next controller
}
else
NSAssert(FALSE, @"Unknown controller type");
}
这一切都假定你要创建和你之间跳跃破坏控制器。 我一般这样做,而是用一个模型对象来存储我的数据,所以我保持我想要的任何数据。
你说你不想做这个“不重新分配和更换UIViews”:如果是这样,你也可以改变上面的代码创建两个子视图控制器的前期和改变过渡到它们之间简单地跳:
- (void)viewDidLoad
{
[super viewDidLoad];
// this is my model, where I store data used by my view controllers
_model = [[MyModel alloc] init];
// let's create our first view controller
_controller0 = [[OneViewController alloc] initWithNibName:@"OneViewController" bundle:nil];
_controller0.model = _model;
[self addChildViewController:_controller0];
[self configureChild:_controller0];
[_controller0 didMoveToParentViewController:self];
// let's create our second view controller
_controller1 = [[OneViewController alloc] initWithNibName:@"OneViewController" bundle:nil];
_controller1.model = _model;
[self addChildViewController:_controller1];
[self configureChild:_controller1];
[_controller1 didMoveToParentViewController:self];
// let's add the first view and update our navigation bar title and the label of the button accordingly
_currentChildController = _controller0;
[self.view addSubview:_currentChildController.view];
[self updateTitles:_currentChildController];
}
- (void)transitionFrom:(UIViewController *)oldController To:(UIViewController *)newController
{
[self transitionFromViewController:oldController
toViewController:newController
duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self updateTitles:newController];
}
completion:^(BOOL finished){
_currentChildController = newController;
}];
}
- (IBAction)barButtonTouchUpInside:(id)sender
{
UIViewController *newChildController;
if ([_currentChildController isKindOfClass:[OneViewController class]])
{
newChildController = _controller1;
}
else if ([_currentChildController isKindOfClass:[TwoViewController class]])
{
newChildController = _controller0;
}
else
NSAssert(FALSE, @"Unknown controller type");
[self transitionFrom:_currentChildController To:newChildController];
}
我已经看到了两种方式,所以你可以为你做任何工作。
请看这里 。 基本上,你要实现的UIViewController遏制这是iOS5的一项新功能。 以上所提供的链接提供了一些代码和一个github上的项目的链接。
祝好运
Ť
我发现我的问题的解决方案。 此代码适用于iOS 4.x版
[UIView beginAnimations:@"transition" context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown
forView:self.navigationController.view
cache:NO];
[self.navigationController
pushViewController:self.alternateView animated:NO];
[UIView commitAnimations];
尝试
UIViewController* controller1;
UIViewController* controller2;
[controller1 transitionFromViewController:controller1 toViewController:controller2 duration:0.5f options:0 animations:nil completion:nil];
要么
如果navigationtroller的顶部 - 控制器1然后
UINavigationController* nav;
[nav pushViewController:controller2 animated:YES];