9

我有两个视图需要模态显示,一个接一个。如果我们连续关闭并显示,这将不起作用,如下所示:

[rootController dismissModalViewControllerAnimated: YES];
[rootController presentModalViewController: psvc animated: YES];

第二个模态视图根本不显示。

我见过一个类似这样的修复:

[rootController dismissModalViewControllerAnimated: YES];
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[self performSelector: @selector(seekModal) withObject: nil afterDelay: 0.5];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];

问题是这不会一直有效(有时所需的延迟会更好)。

另一个可能的解决方法是消除动画:

[rootController dismissModalViewControllerAnimated: NO];
[rootController presentModalViewController: psvc animated: YES];

但我真的很想保留动画,以保持第一个模态不碍事的感觉。有什么建议么?

4

7 回答 7

17

编辑: 在 iOS5+ 中执行此操作的“正确”机制是使用该– dismissViewControllerAnimated:completion:方法,并从完成块呈现顺序视图控制器。


模态显示的视图控制器将在模态关闭动画完成后调用其 viewDidDisappear:animated: 方法。AFIK 这是唯一可以挂钩以启动后续 presentModalViewController:animated: 调用的地方。

我有一个用于呈现模态视图控制器的类,它通过在解除完成后对呈现视图控制器的回调来实现您正在寻找的逻辑。要使用这个类,只需分配/初始化一个实例并使用普通的 presentViewController:animated: 调用来呈现。在呈现视图控制器上实现以下方法:

- (void) modalViewControllerDidDismiss:(UIViewController *)modalViewController

这将在模态视图控制器消失后立即调用,此时您可以呈现一个新的模态视图控制器。

还有一件好事——因为这个类是 UINavigationController 的一个特化,你可以根据需要配置导航栏的开/关。该类还具有显示关闭按钮的内置逻辑,如您所愿。

这是类定义:

@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

并且,这是你如何使用它(在一些普通视图控制器的上下文中):

- (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];
}

并且,这里是解除回调方法,它提供了一个新的模式视图控制器:

- (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];
}
于 2010-11-22T17:18:16.943 回答
0

rootController 可以知道它上面的最后一个模态视图控制器何时消失,因为它会收到一个 viewDidAppear:。您是否尝试过将后续视图控制器的 presentModalViewController: 链接到该控制器?

于 2010-10-13T00:42:36.660 回答
0

我发现使用模态视图的 -viewDidDissapear 来调用呈现视图控制器工作的方法非常好。一个好处是能够延迟模态视图控制器上的释放。请发布我可以做的任何改进。我创建这个协议的灵感来自于 iOS 5 的 "dismissViewControllerAnimated:completion:" 除了 UIViewController。我希望在 iOS 4.3 中有这个功能。


PresentorDelegateProtocol.h

@protocol PresentorDelegateProtocol <NSObject>
@optional

/* 

Extra protocol methods defined in protocol for flexibility.  
Main methods are:
- (void)dismissPresentingModalViewController:(id)modalView animated:(BOOL)animated; 
- (void)modalViewDissapeared:(id)modalView;  //used in modal view's -viewDidDissapear

*/

- (void)dismissPresentingModalViewController:(id)modalView animated:(BOOL)animated;
- (void)modalViewDissapeared:(id)modalView; 

// use the block in this method send messages to save state, etc.  This is the one I like to use.
- (void)dismissPresentingModalViewController:(id)modalView animated:(BOOL)animated withBlock:(void(^)())block;

// use in other classes that are not controlling dismissal of the modal view
- (void)executeBlockOnModalDissapearance: (void(^)())block;

@end

PresentingViewController.h

#import "PresentorDelegateProtocol.h"
@interface PresentingViewController : UIViewController <PresentorDelegateProtocol>
- (void)showModalVC;
@end

模态视图控制器.h

#import "PresentorDelegateProtocol.h"
@interface ModalViewController : UIViewController
@property (nonatomic, assign) id <PresentorDelegateProtocol> presentorDelegate;
- (void)close;
@end

PresentingViewController.m

#import "PresentingViewController.h"
#import "ModalViewController.h"
@implementation PresentingModalViewController
- (void)showModalVC
{
    ModalViewController *modalVC = [[ModalViewController alloc] initWithNibName:@"ModalViewController" bundle:nil];
    modalVC.presentorDelegate = self;
    [self presentModalViewController:modalVC animated:YES];
}
- (void)dismissPresentingModalViewController:(id)modalView animated:(BOOL)animated
{
    if ([modalView isKindOfClass:[ModalViewController class]]) {
        NSLog(@"Can invoke based on class"); 
    }
    [self dismissModalViewControllerAnimated:animated];    
}
- (void)dismissPresentingModalViewController:(id)modalView animated:(BOOL)animated withBlock:(void(^)())block
{
    block();  
    /* execute block before or after calling to dismiss modal view */
    [self dismissPresentingModalViewController:modalView animated:animated];
    //block();
}
- (void)modalViewDissapeared:(id)modalView
{
    if ([modalView isKindOfClass:[ModalViewController class]]) {
        NSLog(@"Do stuff based on class.");
    }
}
- (void)executeBlockOnModalDissapearance: (void(^)())block
{
    block();
    NSLog(@"This delay's dealloc on modal view until block completes");
}
@end

模态视图控制器.m

#import "ModalViewController.h"
@implementation ModalViewController
@synthesize presentorDelegate;

- (void)close
{
    if (1 == 0 /*need to do something before dealloc*/){
        [self.presentorDelegate dismissPresentingModalViewController:self animated:YES withBlock:^{
            NSLog(@"Do stuff with block.  Save, animate, etc");
        }];

    } else {
        [self.presentorDelegate dismissPresentingModalViewController:self animated:YES];
    }
}

- (void)viewDidDisappear:(BOOL)animated
{
    if (1 == 0 /*stuff to do*/){
        [self.presentorDelegate executeBlockOnModalDissapearance:^{
        // do stuff before modal view is deallocated
        }];
    }
    [self.presentorDelegate modalViewDissapeared:self];

    presentorDelegate = nil;
    [super viewDidDisappear:animated];
}
@end;
于 2012-02-11T22:59:16.157 回答
0

如果您真的想将多个视图动画链接在一起,我实际上建议您自己处理动画逻辑。这不是太棘手,然后您可以对视图的呈现方式进行细粒度控制。我刚刚在这里为另一个问题写了类似的东西:

iOS——如何控制模态视图控制器的大小?

您可以只为视图设置动画,关闭视图,当调用 animationDidStop 选择器时,为您的第二个视图设置动画。这样做的好处是您还可以使用视图不透明度和动画方向,以及准确地决定视图应该出现的时间。例如,当第一个视图滑开时,您可以让第二个视图滑到第一个视图之上;无需等待第一个完成其动画。

于 2010-11-20T15:09:01.893 回答
0

您的问题是否与“在模态视图中显示模态视图”有关?我在这里发布了一个答案: iPhone modal view inside another modal view?

于 2010-11-20T21:17:35.243 回答
0

我为这样的事情找到的最佳解决方案(如果它们都是父视图的相同子视图)是将它们的视图修补到启用分页的 UIScrollView 上,(您可以在底部添加一个页面控件以使其清晰并用于导航) 然后将控制器的视图添加到页面视图中,因为它们出现在屏幕上,当它们离开屏幕时删除。

如果控制器依赖于这个被调用,您可能还必须虚拟调用 -viewWillAppear 和 -viewWillDisappear。当你全部编码的时候感觉有点hack-ish,但是一旦你让它工作起来,它看起来很流畅自然,并且没有任何与动画一个视图相关的等待,然后动画下一个它一次离开了。

于 2010-11-21T17:03:46.760 回答
0
// present modal view inside another presented modal view

    FirstViewController *firstVC = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: firstVC];

    // Note: you can use your viewcontroller instead self.window.rootViewController

    [self.window.rootViewController presentViewController:navController animated:YES completion:^{
                //code...
                    SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

                    [navController presentViewController: secondVC animated:YES completion:nil];

                }
            }];
于 2013-03-14T06:13:09.207 回答