2012-01-06 103 views
1

我的應用程序允許用戶在兩​​個不同的模態視圖控制器(用於兩種不同類型的數據輸入)之間切換。下面的代碼用來工作(在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

我碰到的地方,如果不這樣做的前一個消失模態視圖控制器將不會顯示之前的問題(這是與動畫,你不使用 - 但可能是相似的)。我通過介紹viewDidDisappear中的「其他」模式控制器來解決這個問題。可能值得一試,但我懷疑這是「正確」的答案... – 2012-01-06 23:07:03

+0

是的,這聽起來似乎合理 - 我會試試(但可能在週一)。謝謝。當我最初編碼它時,我記得它不適用於動畫 - 我可能遇到了一個隨後在iOS 5中破解的邊緣案例。 – NickHowes 2012-01-06 23:51:11

+0

似乎iOS 4.3和5之間的區別在於iOS 5中的parentViewController是零 - 不確定爲什麼會發生變化,但我重寫了代碼以使用viewDidAppear,現在一切正常。 – NickHowes 2012-01-07 13:17:58

回答

3

傑夫海在他的評論中完全正確,除了一件事。您應該在視圖控制器的-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 

編輯:
現在,如果要在兩個模態的視圖控制器之間傳遞數據,就可以使用一個委託。

// 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 2012-01-07 13:16:10

+0

使用segue更容易,併爲準備segue方法設置下一個控制器上的值 – 2013-02-05 20:02:29

相關問題