简体   繁体   English

iPhone-用另一个模式视图覆盖模式视图

[英]iPhone - Cover a modal View with another modal View

I have a main view. 我有一个主要观点。
From that main view I show a modal view (MV1). 从该主视图中,我显示了一个模态视图(MV1)。
MV1 may show another modal View (MV2). MV1可能显示另一个模态视图(MV2)。
From MV2, I may show another modal view (MV3). 从MV2,我可以显示另一个模式视图(MV3)。

All that MV are shown animated. 所有的MV都显示为动画。

What I want, is to be able to first display (animated) the next modal view (MVx+1) before "killing" the previous one (MVx). 我想要的是能够在“杀死”上一个模式视图(MVx)之前先显示(动画)下一个模式视图(MVx + 1)。

If I dismiss (animated) MVx before showing MVx+1 : MVx+1 does not appear. 如果在显示MVx + 1之前关闭了MVx(动画),则不会出现MVx + 1。
If I dismiss (non-animated) MVx before showing MVx+1 : MVx-1 is seen. 如果在显示MVx + 1之前关闭了MVx(未动画),则会看到MVx-1。
If I show MVx+1 before dismissing (non-animated) MVx : MVx+1 does not appear. 如果我在关闭(未动画)之前显示MVx + 1,则MVx:不会出现MVx + 1。

How may I do ? 我该怎么办?

Some code sample would help if you have time, but just a detailed explanation would be enough. 如果有时间,一些代码示例会有所帮助,但仅提供详细的说明就足够了。

According to the Apple docs, the accepted way to dismiss modal views is by letting the parent controller (ie, the view controller that created the modal view) do the dismissing. 根据Apple文档,消除模态视图的公认方法是让父控制器(即创建模态视图的视图控制器)进行消除。 The best way to do this is by setting the parent controller as the delegate of the modal view controller. 最好的方法是将父控制器设置为模态视图控制器的委托。 The idea here is that the modal controller tells its parent that it's ready to be dismissed, and the parent decides what course of action to take from there. 这里的想法是,模态控制器告诉其父级已准备好被解散,然后父级决定从那里采取什么行动。

In order to do this, you have to create a delegate protocol for the modal view controller that the parent controller implements. 为此,您必须为父控制器实现的模式视图控制器创建一个委托协议。 In your case, you can create a protocol at the top of each of your .h files for your modal views to do this (or a single protocol in a separate file if all of the modal views can use the same method for dismissal). 在您的情况下,您可以在每个.h文件的顶部为模态视图创建一个协议来执行此操作(如果所有模态视图可以使用相同的方法进行解雇,则可以在单独的文件中创建一个协议)。 For example: 例如:

@protocol MYModalViewDelegate <NSObject>
    -(void)dismiss;
@end

Next, in each of your modal view controllers, create an instance variable for the delegate: 接下来,在每个模式视图控制器中,为委托创建一个实例变量:

@interface MYModalViewController1 : UIViewController {
    id<MYModalViewDelegate> delegate;
}

When you display a modal view from a current view controller, set the current controller as the delegate. 从当前视图控制器显示模式视图时,将当前控制器设置为委托。

MYModalViewController1 * mvc1 = [[MYModalViewController1 alloc] initWithNibName:@"MYModalViewController1" bundle:nil];
mvc1.delegate = self;
[self presentModalViewController:mvc1 animated:YES];
[mvc1 release];

When you want to release the current modal controller, have the modal view controller call the appropriate protocol method on its delegate: 当您要释放当前的模式控制器时,让模式视图控制器在其委托上调用适当的协议方法:

[self.delegate dismiss];

Now, the delegate can handle where to go next. 现在,委托人可以处理下一步的工作。 In your case, you can close MV2 automatically when MV3 closes by calling [self.delegate dismiss] in MV3, then implement dismiss in MV2 as: 你的情况,你可以关闭MV2时自动关闭MV3通过调用[self.delegate dismiss]在MV3,然后实现dismiss在MV2为:

-(void)dismiss {
    [self dismissModalViewControllerAnimated:YES];
    [self.delegate dismiss];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM