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

发布于 2024-12-11 08:26:11 字数 409 浏览 0 评论 0原文

我有一个主要观点。
从该主视图中,我显示了一个模态视图 (MV1)。
MV1 可能会显示另一个模态视图 (MV2)。
从 MV2 开始,我可能会展示另一个模态视图(MV3)。

所有MV均以动画形式呈现。

我想要的是能够在“杀死”前一个模态视图 (MVx) 之前首先显示(动画)下一个模态视图 (MVx+1)。

如果我在显示之前关闭(动画)MVx MVx+1 : MVx+1 不出现。
如果我在显示 MVx+1 之前关闭(非动画)MVx:会看到 MVx-1。
如果我在关闭(非动画)MVx 之前显示 MVx+1:MVx+1 不会出现。

我该怎么做?

如果您有时间,一些代码示例会有所帮助,但只需详细的解释就足够了。

I have a main view.
From that main view I show a modal view (MV1).
MV1 may show another modal View (MV2).
From MV2, I may show another modal view (MV3).

All that MV are shown animated.

What I want, is to be able to first display (animated) the next modal view (MVx+1) before "killing" the previous one (MVx).

If I dismiss (animated) MVx before showing MVx+1 : MVx+1 does not appear.
If I dismiss (non-animated) MVx before showing MVx+1 : MVx-1 is seen.
If I show MVx+1 before dismissing (non-animated) MVx : MVx+1 does not appear.

How may I do ?

Some code sample would help if you have time, but just a detailed explanation would be enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各空 2024-12-18 08:26:11

根据Apple文档,消除模态视图的可接受方法是让父控制器(即创建模态视图的视图控制器)进行消除。最好的方法是将父控制器设置为模式视图控制器的委托。这里的想法是,模态控制器告诉其父级它已准备好被解雇,并且父级决定从那里采取什么行动方案。

为此,您必须为父控制器实现的模式视图控制器创建委托协议。在您的情况下,您可以在每个 .h 文件的顶部为模态视图创建一个协议来执行此操作(或者如果所有模态视图都可以使用相同的方法来关闭,则在单独的文件中创建一个协议)。例如:

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

接下来,在每个模态视图控制器中,为委托创建一个实例变量:

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

当您从当前视图控制器显示模态视图时,将当前控制器设置为委托。

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

当您想要释放当前模态控制器时,让模态视图控制器在其委托上调用适当的协议方法:

[self.delegate dismiss];

现在,委托可以处理下一步要去哪里。在您的情况下,您可以在 MV3 关闭时通过调用 MV3 中的 [self.delegate Dismiss] 自动关闭 MV2,然后在 MV2 中实现 dismiss 如下:

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

According to the Apple docs, the accepted way to dismiss modal views is by letting the parent controller (i.e., the view controller that created the modal view) do the dismissing. 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). 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:

-(void)dismiss {
    [self dismissModalViewControllerAnimated:YES];
    [self.delegate dismiss];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文