ViewController转换

时间:2012-07-30 13:36:36

标签: iphone objective-c ios transition

我有代码:

ListViewController * listViewController = [[ListViewController alloc] initWithNibName:@"ListViewController" bundle:nil];    

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[self viewWillDisappear:YES];
[listViewController viewWillAppear:YES];

self.view.hidden = YES;
listViewController.view.hidden = NO;


[self viewDidDisappear:YES];
[listViewController viewDidAppear:YES];
[UIView commitAnimations];    

但它不起作用,并且listViewController没有显示(请问,有人可以告诉我这个问题的解决方案吗?

3 个答案:

答案 0 :(得分:0)

删除不必要的代码,然后写下来......

ListViewController * listViewController = [[ListViewController alloc] initWithNibName:@"ListViewController" bundle:nil];    
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[self.view addSubview:listViewController.view];
[UIView commitAnimations];

答案 1 :(得分:0)

尝试类似:

UIViewAnimationOptions ops = UIViewAnimationOptionTransitionFlipFromRight;
NSArray *temp = [[NSBundle mainBundle] loadNibNamed:@"NameOfNib" owner:self options:nil];
UIView* newView = [[temp objectAtIndex:0] view];
[UIView transitionFromView:self.view toView:newView duration:1.5 options:ops completion:nil];
self.view = newView; //Lets you control the new view from the current controller (you might want to save a reference to the old one if you need to change back)

正如meronix所说,Apple不鼓励使用非基于块的动画。对于较新的iOS版本。上述方法是“批准”的方式。

您知道,viewWillAppearviewDidDisappear和类似的方法不是您调用以使视图执行操作的方法。当这些事情发生时,它们会被自动调用。

你的代码有一些误解;我在下面对它们发表了评论

//This looks fine (depending on what is in the nib)
ListViewController * listViewController = [[ListViewController alloc] initWithNibName:@"ListViewController" bundle:nil]; 

//Normally I use these to move things around, not change the view
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];

[self viewWillDisappear:YES]; //These two methods aren't things that you call
[listViewController viewWillAppear:YES];

self.view.hidden = YES; //If you're flipping or otherwise moving a view out of 
listViewController.view.hidden = NO; //sight then you don't need to hide/unhide views

[self viewDidDisappear:YES]; //Same as above, you don't call these
[listViewController viewDidAppear:YES];
[UIView commitAnimations]; 

答案 2 :(得分:0)

它不起作用!

您只需创建并分配UIViewController,但不要将其推送到任何堆栈上,或将其视图添加到可见视图中。

当你将listViewController.view.hidden设置为no时,你并没有神奇地在屏幕上显示它:你需要将它的视图添加到已经在屏幕上的视图(或窗口)......

不推荐使用ps beginAnimation:使用块动画代替......