2011-07-02 14 views
3

私のiPhoneに既に存在するUIViewControllerの上にUIViewControllerを追加する最良の方法は何ですか?私が知っている2つの方法があります。しかし、より良い方法がありますか?これらのどれがいいですか?別の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

あなたが望むものによって異なります。他の方法よりも良い方法はありません。すべてがちょうど...方法です。

1

ビューコントローラを表示するという要件によって異なります。 ナビゲーションスタックにコントローラを1つ以上押し込むことができます。 pushViewController:とするときpresentModalViewController:を使用するときのため

[self.navigationController pushViewController:anotherViewController animated:YES]; 

チェックAPPLフォーラムのポスト。

pushViewController versus presentModalViewController

presentModalViewController vs. pushViewController

2

これは、あなたがそれを実装する方法によって異なります。 View Controllerを表示し、既存のトランジションを使用して削除する場合は、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];を使用して、しかし、 Yea presentModalViewControllerは、addSubviewであるかどうかに関わらず、アニメーションのデフォルトオプションを与えます。そのために少し作業する必要があります。

関連する問題