2012-01-17 7 views
0

私は2つの目に見える非常にシンプルなiPhoneアプリケーションをまとめようとしています。ビューをスワップする方法

私はRootViewController、View1Controller、およびView2Controllerを持っています。私のRootViewControllerには、あるビューを別のビューに切り替えるボタンがあります。そうですね...

- (IBAction)flipView:(id)sender 
{ 
    if (self.view2Controller.view.superview == nil) 
    { 
     if (self.view2Controller == nil) 
     { 
      self.view2Controller = [[View2Controller alloc] initWithNibName:@"View2Controller" bundle:nil]; 
     } 
     [view1Controller.view removeFromSuperview]; 
     [self.view insertSubview:self.view2Controller.view atIndex:0]; 
    } 
    else 
    { 
     if (self.view1Controller == nil) 
     { 
      self.view1Controller = [[View1Controller alloc] initWithNibName:@"View1Controller" bundle:nil]; 
     } 
     [view2Controller.view removeFromSuperview]; 
     [self.view insertSubview:self.view1Controller.view atIndex:0]; 

    } 
} 

それは素晴らしいです。 RootViewのボタンがこのメソッドに当たってビューをスワップしても問題はありません。

私は、View1ControllerとView2Controllerの両方のボタンに接続して、他のものにリンクしていますが、問題があります。私は限り

...など
- (IBAction)swap:(id)sender { 

    [self.view removeFromSuperview]; 
    // This is where I get stuck 
} 

を得ることができた

私の周り探してみましたが、私はこのような状況で何をすべきかうまくできません。スーパークラスのビューで新しいビューを読み込むにはどうしたらいいですか?それは私がここでやりたいことさえあるの?コードを適切な場所にドロップしていますか?

ご協力いただければ幸いです。

答えて

0

一方のビューコントローラのビューを他方のビューコントローラに直接追加しないでください。モーダルビューコントローラを使用してそれをやってみてください。View1Controller.mで

:View2Controller.mで

- (void) flipView:(id)sender 
{ 
    View2Controller *vc2 = [[View2Controller alloc] init]; 
    [self presentModalViewController:vc2 animated:YES]; 
    [vc2 release]; 
} 

- (void) swap:(id)sender 
{ 
    [self dismissModalViewControllerAnimated:YES]; 
} 
関連する問題