连续显示的modalViews的正确方法 [英] Correct way of showing consecutive modalViews
问题描述
我有需要显示模态,一前一后两个视图。如果我们解雇,并显示连续,这样这不工作:
I have two views that need to be shown modally, one after the other. This doesn't work if we dismiss and show consecutively, like this:
[rootController dismissModalViewControllerAnimated: YES];
[rootController presentModalViewController: psvc animated: YES];
第二个模式视图根本不露面。
The second modal view simply doesn't show up.
我已经看到了修复程序是这样的:
I've seen a fix that was something like this:
[rootController dismissModalViewControllerAnimated: YES];
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[self performSelector: @selector(seekModal) withObject: nil afterDelay: 0.5];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
的问题是,这将无法工作的所有时间(所需要的延迟优越,有时)
The problem is that this won't work all the time (the delay needed is superior, sometimes).
另一种可能的解决将是消除动画:
Another possible fix would be to eliminate the animation:
[rootController dismissModalViewControllerAnimated: NO];
[rootController presentModalViewController: psvc animated: YES];
但我真的很想让动画,以保持手感,第一个模式是出路。有什么建议?
But I'd really like to keep the animation, to keep the feel that the first modal is out of the way. Any suggestions?
推荐答案
编辑:正确的机制的iOS5 +这样做将是使用 - dismissViewControllerAnimated:完成:
方法,和present从完成的块顺序视图控制器
The "correct" mechanism to do this in iOS5+ would be to use the – dismissViewControllerAnimated:completion:
method, and present the sequential view controller from the completion block.
这正显示模态都有其viewDidDisappear的ViewController中:动画:调用一次模态-解雇动画手段齐全。 AFIK这是你可以钩启动后续presentModalViewController唯一的地方:动画:呼叫
The viewcontroller that is being shown modally will have its viewDidDisappear:animated: method called once the modal-dismissal-animation is complete. AFIK this is the only place you can hook to initiate a subsequent presentModalViewController:animated: call.
我有我使用presenting模式视图控制器的类,它实现你通过回调至presenting视图控制器寻找,一旦解雇完整的逻辑。要使用这个类,简单地分配/初始化使用正常的presentViewController一个实例和present:动画:打电话。实施presenting视图控制器下面的方法:
I have a class that I use for presenting modal view controllers and it implements the logic you're looking for via a callback to the presenting view controller once the dismissal is complete. To use this class, simply alloc/init an instance and present using the normal presentViewController:animated: call. Implement the following method on the presenting view controller:
- (void) modalViewControllerDidDismiss:(UIViewController *)modalViewController
这将在一次模态视图控制器走了被调用,你可以在这个时候present一个新的模式视图控制器。
This will be called at once the modal view controller is gone, and you can present a new modal view controller at this time.
一个很好的事情太多 - 因为这个类的UINavigationController的一个特例,可以开启/关闭你喜欢配置的导航栏。该类还具有内置的逻辑来呈现解雇按钮,只要你喜欢。
One nice thing too - since this class is a specialization of UINavigationController, you can configure the navigationBar on/off as you like. The class also has built-in logic to show a dismiss button, as you like.
下面的类定义:
@protocol TSModalViewControllerDelegate
- (void) modalViewControllerDidDismiss: (UIViewController*) modalViewController;
@end
@interface TSModalViewController : UINavigationController
{
UIViewController* _originalParentViewController;
}
@property BOOL dismissButtonHidden;
- (id) initWithViewController: (UIViewController*) vc;
- (id) initWithClass: (Class) c;
- (id) initWithClass: (Class) c nibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
@end
和类实现:
@implementation TSModalViewController
@synthesize dismissButtonHidden;
- (id) initWithViewController: (UIViewController *)vc
{
return [super initWithRootViewController: vc];
}
- (id) initWithClass:(Class)c
{
UIViewController* vc = [[[c alloc] init] autorelease];
return [self initWithViewController: vc];
}
- (id) initWithClass: (Class) c nibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
UIViewController* vc = [[[c alloc] initWithNibName:nibNameOrNil bundle:nibBundleOrNil] autorelease];
return [self initWithViewController: vc];
}
- (void) viewDidAppear: (BOOL) animated
{
[super viewDidAppear: animated];
[_originalParentViewController release];
_originalParentViewController = [self.parentViewController retain];
if (!self.dismissButtonHidden)
{
UIBarButtonItem* dismissButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemStop
target: self
action: @selector(onDismiss:)] autorelease];
UIViewController* rootViewController = [self.viewControllers objectAtIndex:0];
rootViewController.navigationItem.leftBarButtonItem = dismissButton;
self.navigationBarHidden = NO;
}
}
- (void) viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear: animated];
if ( [_originalParentViewController respondsToSelector: @selector(modalViewControllerDidDismiss:)] )
{
[_originalParentViewController performSelector: @selector(modalViewControllerDidDismiss:) withObject: self];
}
}
- (void) dismissModalViewControllerAnimated:(BOOL)animated
{
return [self.parentViewController dismissModalViewControllerAnimated: animated];
}
- (void) onDismiss: (id) sender
{
[self.parentViewController dismissModalViewControllerAnimated: YES];
}
- (void) didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void) viewDidUnload
{
[super viewDidUnload];
}
- (void)dealloc
{
[_originalParentViewController release];
[super dealloc];
}
@end
和,这里是你如何使用它(在一些普通视图控制器的上下文中):
and, here's how you can use it (in the context of some normal view controller):
- (void) onShowIt:(id)sender
{
TSModalViewController* mvc = [[[TSModalViewController alloc] initWithClass: [MyModalViewController class] nibName: @"MyModalViewController" bundle:nil] autorelease];
mvc.dismissButtonHidden = YES; // set to no if you don't want an "automatic" close button
[self presentModalViewController: mvc animated: YES];
}
和,这里是解雇回调方法,presents一个新的模式视图控制器:
and, here is the dismissal callback method, which presents a new modal view controller:
- (void) modalViewControllerDidDismiss:(UIViewController *)modalViewController
{
MyModalViewController* vc = [[[MyModalViewController alloc] initWithNibName: @"MyModalViewController" bundle:nil] autorelease];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
TSModalViewController* mvc = [[[TSModalViewController alloc] initWithViewController: vc] autorelease];
[self presentModalViewController: mvc animated: YES];
}
这篇关于连续显示的modalViews的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!