在模态视图控制器之间切换
我的应用程序允许用户在两个不同的模态视图控制器(用于两种不同类型的数据输入)之间切换。下面的代码用来工作(在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和更早的版本上工作)?
杰夫海在他的评论中完全正确,除了一件事。您应该在视图控制器的-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
我开悟了。谢谢!我实现了类似的东西,它运行的很好。 – NickHowes 2012-01-07 13:16:10
使用segue更容易,并为准备segue方法设置下一个控制器上的值 – 2013-02-05 20:02:29
我碰到的地方,如果不这样做的前一个消失模态视图控制器将不会显示之前的问题(这是与动画,你不使用 - 但可能是相似的)。我通过介绍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