模态视图控制器不调用呈现视图控制器的dismissModalViewControllerAnimated:方法

发布于 2024-12-27 17:13:33 字数 612 浏览 3 评论 0原文

在我的模态视图控制器中,我有一个按钮处理方法,其中包括

[self dismissModalViewControllerAnimated: YES];

在呈现视图控制器中,我重写dismissModalViewControllerAnimated:如下:

-(void) dismissModalViewControllerAnimated: (BOOL)animated
{
  NSLog(@"dismiss");
  [super dismissModalViewControllerAnimated: animated];
}

当触摸按钮时,按钮处理方法被调用,但dismissModalViewControllerAnimated:重写似乎没有被调用: NSLog(@"解雇");语句不会被调用,并且方法内的断点不会被命中。

我尝试过

[[self presentingViewController] dismissModalViewControllerAnimated: YES];

,但这也不起作用。然而,模态视图控制器确实被忽略了。

知道可能出了什么问题吗?

In my modal view controller I have a button handling method that includes

[self dismissModalViewControllerAnimated: YES];

In the presenting view controller I override dismissModalViewControllerAnimated: as follows:

-(void) dismissModalViewControllerAnimated: (BOOL)animated
{
  NSLog(@"dismiss");
  [super dismissModalViewControllerAnimated: animated];
}

When the button is touched, the button handling method gets called, but the dismissModalViewControllerAnimated: override does not seem to get called: the NSLog(@"dismiss"); statement isn't called, and a breakpoint inside the method doesn't get hit.

I tried

[[self presentingViewController] dismissModalViewControllerAnimated: YES];

but that didn't work either. However, the modal view controller does get dismissed.

Any idea what might be going wrong?

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

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

发布评论

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

评论(3

┊风居住的梦幻卍 2025-01-03 17:13:33

这通常是通过将呈现视图控制器声明为模式视图控制器的委托来处理的。然后,模态 VC 在呈现的 VC 中调用委托方法来消除它创建的模态转换。

示例:

Modal VC.h:

@protocol ModalViewControllerDelegate
-(void)dismissMyModalViewController;
@end

Modal VC.m:

// When you want to dismiss the Modal VC
[delegate dismissMyModalViewController]; 

呈现 VC.h:

// Make sure to #import ModalVC.h
@property (nonatomic, retain) id <ModalViewControllerDelegate> delegate;

呈现 VC.m:

-(void)dismissMyModalViewController {
    [self dismissModalViewControllerAnimated:YES];
}

This is normally handled by declaring your presenting view controller as a delegate for your modal view controller. The modal VC then called a delegate method in the presenting VC to dismiss the modal transition it created.

Example:

Modal VC.h:

@protocol ModalViewControllerDelegate
-(void)dismissMyModalViewController;
@end

Modal VC.m:

// When you want to dismiss the Modal VC
[delegate dismissMyModalViewController]; 

Presenting VC.h:

// Make sure to #import ModalVC.h
@property (nonatomic, retain) id <ModalViewControllerDelegate> delegate;

Presenting VC.m:

-(void)dismissMyModalViewController {
    [self dismissModalViewControllerAnimated:YES];
}
套路撩心 2025-01-03 17:13:33

来自 iOS 6 编程,作者:Matt Neuburg

在iPad上,当呈现的视图控制器的modalPresentationStyle是UIModalPresentationCurrentContext时,必须决定哪个视图控制器应该是呈现的视图控制器的presentingViewController。这将确定哪个视图将被所呈现的视图控制器的视图替换。此决定涉及另一个 UIViewController 属性,definePresentationContext(BOOL)。从presentViewController:animated:completion: 被发送到的视图控制器开始,我们沿着父视图控制器链向上走,寻找definePresentationContext 属性为YES 的视图控制器。如果我们找到一个,那就是那个;它将是presentingViewController,并且它的视图将被呈现的视图控制器的视图替换。如果我们没有找到,事情就会像呈现的视图控制器的 modalPresentationStyle 是 UIModalPresentationFullScreen 一样工作。

TL;博士
1. 在所需的 presentingViewController 上将 definesPresentationContext 设置为 true
2. 在所需的 presentedViewController 上将 modalPresentationStyle 设置为 UIModalPresentationCurrentContext

from Programming iOS 6, by Matt Neuburg:

On the iPad, when the presented view controller’s modalPresentationStyle is UIModalPresentationCurrentContext, a decision has to be made as to what view controller should be the presented view controller’s presentingViewController. This will determine what view will be replaced by the presented view controller’s view. This decision involves another UIViewController property, definesPresentationContext (a BOOL). Starting with the view controller to which presentViewController:animated:completion: was sent, we walk up the chain of parent view controllers, looking for one whose definesPresentationContext property is YES. If we find one, that’s the one; it will be the presentingViewController, and its view will be replaced by the presented view controller’s view. If we don’t find one, things work as if the presented view controller’s modalPresentationStyle had been UIModalPresentationFullScreen.

TL;DR
1. set definesPresentationContext to true on the desired presentingViewController
2. set modalPresentationStyle to UIModalPresentationCurrentContext on the desired presentedViewController

无法回应 2025-01-03 17:13:33

呈现模态视图控制器的代码包含在 UIViewController 中,而 UIViewController 又包含在 UINavigationController 中。当我打电话

[[self presentingViewController] dismissModalViewControllerAnimated: YES];

[self dismissModalViewControllerAnimated: YES];

解雇消息被发送到 UINavigationController 对象时。

The code that presented the modal view controller was contained in a UIViewController, which was in turn contained in a UINavigationController. When I called

[[self presentingViewController] dismissModalViewControllerAnimated: YES];

or

[self dismissModalViewControllerAnimated: YES];

the dismissal message was being sent to the UINavigationController object.

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