解除ModalViewController动画之谜

发布于 2024-11-14 22:15:25 字数 1458 浏览 4 评论 0原文

我看过很多关于这个主题的帖子,但没有一个能让我清楚地了解正在发生的事情。

我设置了一个涉及两个 UIViewController 的小测试:MainController 和 ModalController。

MainController 上有一个按钮,使用以下简单代码显示模态视图控制器:

ModalController *myModal = [[ModalController alloc] init];
[self presentModalViewController:myModal animated:YES];
[myModal release];

现在,如果我立即从同一代码块中消除此模态控制器,如下一行所示:

[self dismissModalViewControllerAnimated: YES];

模态视图不会消除。

根据此站点上的一些建议,我将 dismissModalViewControllerAnimated 调用放入一个单独的方法中,然后使用以下方法调用该方法:

[self performSelector:@selector(delayedDismissal) withObject:nil 
  afterDelay:0.41];

这有效 - 至少如果我将延迟设置为 0.41 或更大。 .40 或更低,它不起作用。

此时,我假设我正在处理一个需要赶上自身的运行循环,因为缺乏更好的描述。不幸的是,它不是很稳定。

因此,对于下一个测试,我让delayedDismissal什么也不做——它只提供延迟——并在原始块中重新插入dismissModalViewControllerAnimated回调,这样我的代码现在看起来像这样:

ModalController *myModal = [[ModalController alloc] init];
[self presentModalViewController:myModal animated:YES];
[myModal release];
self performSelector:@selector(delayedDismissal) withObject:nil 
      afterDelay:0.41]; // to create the false delay
[self dismissModalViewControllerAnimated: YES];

...现在dismissModalViewControllerAnimated无论我使用多长时间的延迟,都不再起作用。

那么,这里发生了什么?我意识到,像其他人一样,我可以通过各种解决方法来实现我的目标,包括使用委托等。但我真的认为,对于遇到此问题的每个人来说,彻底了解问题和解决问题是有好处的。这种情况的正确解决方案。顺便说一句,此场景的一个用例是以模态方式呈现加载屏幕,其中用户与该屏幕没有交互;它只是用来呈现信息,同时阻止用户采取行动。

I've seen a number of posts on this subject, but none that leave me with a clear understanding of what is happening.

I've set up a small test involving two UIViewControllers: MainController and ModalController.

MainController has a button on it that presents a modal view controller using the following simple code:

ModalController *myModal = [[ModalController alloc] init];
[self presentModalViewController:myModal animated:YES];
[myModal release];

Now, if I immediately dismiss this modal controller from within the same block of code, as per this next line:

[self dismissModalViewControllerAnimated: YES];

The modal view does not dismiss.

Following some suggestions on this site, I put the dismissModalViewControllerAnimated call in a separate method, which I then called with:

[self performSelector:@selector(delayedDismissal) withObject:nil 
  afterDelay:0.41];

This works - at least if I make the delay 0.41 or greater. .40 or less and it doesn't work.

At this point, I'm assuming I'm dealing here a run-loop that needs to catch up with itself, for lack of a better description. It's not very stable, unfortunately.

So, for the next test, I make the delayedDismissal do nothing - it only serves to provide a delay - and re-insert the dismissModalViewControllerAnimated call back in the original block, such that my code now looks like this:

ModalController *myModal = [[ModalController alloc] init];
[self presentModalViewController:myModal animated:YES];
[myModal release];
self performSelector:@selector(delayedDismissal) withObject:nil 
      afterDelay:0.41]; // to create the false delay
[self dismissModalViewControllerAnimated: YES];

...now the dismissModalViewControllerAnimated doesn't work again, no matter how long a delay I use.

So, what is happening here? I realize, like others, I can achieve my goal through assorted workarounds, including the use of a delegate, etc. But I really think it would be good for everyone who encounters this issue to walk away with a thorough understanding of both the problem and the proper solution for this scenario. Incidentally, one use case for this scenario is to present a loading screen modally where the user has no interaction with that screen; it's just being used to present information while blocking the user from taking actions.

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

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

发布评论

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

评论(1

羞稚 2024-11-21 22:15:25

该视图是动画的,因此只要它是动画的,调用解雇就不起作用。

同样,在您尝试的第二件事中,您正在调用“延迟”,但您实际上正在做的是说以下内容:“好吧,这是这个可爱的方法,您可以在 0.41 秒后执行吗?谢谢,同时,调用此方法..”

应该通过用户界面单击按钮来关闭模态视图控制器,那么您为什么要首先尝试此操作呢?

The view is animating, thus as long as it is animating calling dismiss won't work.

Also in the second thing you tried, you are calling a "delay" but what you are actually doing is saying the following: "Ok, here is this cute method, can you execute that 0.41 seconds later? thanks, in the mean time, call this method.."

Dismissing a modal view controller should be done through the userinterface, by clicking a button, so why are you trying this in the first place?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文