2011-07-02 37 views
3

在我的iPhone上已经存在的UIViewController的顶部添加UIViewController的最佳方式是什么?我知道有两种方法。但是有更好的方法吗?如果不是,哪一个更好?将UIViewController添加到另一个UIViewController的最佳方式

1. [self presentModalViewController:controller animated:NO]; 
2. [self.view addSubview:someController.view]; 

回答

1
[[self view] addSubview: [otherViewController view]]; 

CGRect frame = [[self view] frame]; 

int direction = 1; 
switch (direction) { 
    case 0: // from bottom 
     frame.origin.y = [[self view] frame].size.height; 
     [[otherViewController view] setFrame: frame]; 
     frame.origin.y = 0.0 - [[self view] frame].size.height; 
     break; 
    case 1: // from right 
     frame.origin.x = [[self view] frame].size.width; 
     [[otherViewController view] setFrame: frame]; 
     frame.origin.x = 0.0 - [[self view] frame].size.width; 
     break; 
} 

[UIView animateWithDuration: 1.0 
         delay: 0.0 
        options: UIViewAnimationOptionCurveEaseInOut 
       animations:^{ 
        [[self view] setFrame: frame]; 
       } 
       completion: ^(BOOL finished) { 

       } 
]; 
-1

取决于你想要什么。没有一种方法比另一种更好。所有只是...方式。

2

这取决于你想如何实现它。如果要显示视图控制器并使用现有的转换将其解除,可以使用presentModalViewController。但是,如果您想用一些自定义动画来显示它,则可以使用addSubView。而且,这完全取决于你。

+0

所以,如果我有一个UIViewController,我想添加一个子视图,我怎么能动画它?例如,让UIButton从右侧飞入? – StanLe

+0

@StanLe,你可以使用UIView动画。看到这个链接,http://objcolumnist.com/2009/07/18/simple-uiview-based-animations-on-the-iphone/ – EmptyStack

0

两种方式,几乎是等同的,因为做出的意见堆栈,你可以通过看下面的视图(直到没有碱性),[self removeFromSuperview],当你addSubView,并[self dismissModalViewControllerAnimated:YES];,当溜溜使用[self presentModalViewController:tempView animated:NO];,但是的presentModalViewController,给你动画的默认选项是否与addSubview,你需要工作一点。

相关问题