2012-01-06 81 views
1

私のアプリでは、2種類のモーダルビューコントローラ(2種類のデータ入力用)を切り替えることができます。 (IOSの4.3およびそれ以前に)動作するように使用される以下のコード:モーダルビューコントローラ間の切り替え

UIViewController * parent = current.parentViewController; 
    [current dismissModalViewControllerAnimated:NO]; 
    svc.modalPresentationStyle = UIModalPresentationFormSheet; 
    [parent presentModalViewController:svc animated:NO]; 
    [svc release]; 

が、もはや(IOSの5) - 「現在」ビューコントローラは退けたが、「SVC」が提示されません。

なぜそれが壊れたのか(つまり何が間違っていたか) "正しい"(それは5.0と4.3以前のバージョンで動作するように)どのようにしたらよいでしょうか?

+2

私は前のものが(これは、アニメーションとあった遠ざかって行われていない場合は、モーダルビューコントローラが表示されない場所の前の問題に実行しましたあなたは使っていませんが、似ているかもしれません)。私はviewDidDisPear:から最初の "他の"モーダルコントローラを提示することで、その周りにいました。一発の価値があるかもしれないが、私はそれが "正しい"答えだとは思わない... –

+0

ええ、それは妥当と思われる - 私はそれを試してみるだろう(しかしおそらく月曜日に)。どうも。私は最初にそれをコード化したときにアニメーションにはうまくいかなかったことを覚えています - おそらく私はおそらくiOS 5で壊れたエッジケースを打ちました。 – NickHowes

+0

iOS 4.3と5の違いは、parentViewControllerがiOS 5ではnilなぜ変わったのですか?viewDidAppearを使うコードを書き直しましたが、今はすべてうまくいきます。 – NickHowes

答えて

3

ジェフ・ヘイは、1つのことを除いて彼のコメントにまったく正しかった。最初にモーダルビューコントローラを提示したビューコントローラの-viewDidAppear:メソッドで行う必要があります。

例:

// MyViewController.h 
@interface MyViewController : UIViewController { 
    BOOL _shouldPresentSecondModalViewController; 
} 
@end 

// MyViewController.m 
@implementation MyViewController 
- (void)viewDidAppear:(BOOL)animated { 
    if(_shouldPresentSecondModalViewController) { 
     UINavigationController *myNavCon; 
     // Code to create second modal navigation controller 
     [self presentModalViewController:myNavCon animated:YES]; 
     _shouldPresentSecondModalViewController = NO; 
    } 
} 

- (void)presentFirstViewController { 
    UINavigationController *myNavCon; 
    // Code to create the first navigation controller 
    _shouldPresentSecondModalViewController = YES; 
    [self presentModalViewController:myNavCon animated:YES]; 
} 
@end 

EDIT:
今、あなたは2つのモーダルビューコントローラ間でデータを渡したい場合、あなたは、デリゲートを使用することができます。

// FirstModalViewControllerDelegate.h 
@protocol FirstModalViewControllerDelegate 
@optional 
- (void)controller:(FirstModalViewControllerDelegate *)vc shouldShowData:(id)anyType; 
@end 

// MyViewController.h 
@interface MyViewController : UIViewController <FirstModalViewControllerDelegate> { 
    id _dataToDisplay; 
} 
@end 

// MyViewController.m 
@implementation MyViewController 
- (void)viewDidAppear:(BOOL)animated { 
    if(_dataToDisplay != nil) { 
     UINavigationController *myNavCon; 
     // Code to create second modal navigation controller 
     [self presentModalViewController:myNavCon animated:YES]; 
     [_dataToDisplay release]; 
     _dataToDisplay = nil; 
    } 
} 

- (void)presentFirstViewController { 
    UINavigationController *myNavCon; 
    FirstModalViewController *myCon; 
    // Code to create the first modal view controller 

    [myCon setDelegate:self]; 

    myNavCon = [[UINavigationController alloc] initWithRootViewController:myCon]; 

    [self presentModalViewController:myNavCon animated:YES]; 
    [myNavCon release]; 
} 

- (void)controller:(FirstModalViewControllerDelegate *)vc shouldShowData:(id)anyType { 
    /* This method will get called if the first modal view controller wants to display 
    some data. If the first modal view controller doesn't call this method, the 
    _dataToDisplay instance variable will stay nil. However, in that case, you'll of 
    course need to implement other methods to, like a response to a Done button, dismiss 
    the modal view controller */ 
    [self dismissModalViewController]; 
    _dataToDisplay = [anyType retain]; 
} 
@end 
+0

私は啓発されています。ありがとう!私は同様のものを実装し、それは美しく動作します。 – NickHowes

+0

セグを使用する方が簡単で、セグのメソッドを準備する際に、次のコントローラーの値を設定する –