傑夫海在他的評論中完全正確,除了一件事。您應該在視圖控制器的-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
我碰到的地方,如果不這樣做的前一個消失模態視圖控制器將不會顯示之前的問題(這是與動畫,你不使用 - 但可能是相似的)。我通過介紹viewDidDisappear中的「其他」模式控制器來解決這個問題。可能值得一試,但我懷疑這是「正確」的答案... – 2012-01-06 23:07:03
是的,這聽起來似乎合理 - 我會試試(但可能在週一)。謝謝。當我最初編碼它時,我記得它不適用於動畫 - 我可能遇到了一個隨後在iOS 5中破解的邊緣案例。 – NickHowes 2012-01-06 23:51:11
似乎iOS 4.3和5之間的區別在於iOS 5中的parentViewController是零 - 不確定爲什麼會發生變化,但我重寫了代碼以使用viewDidAppear,現在一切正常。 – NickHowes 2012-01-07 13:17:58