故事板:使用委托/协议方法关闭 Popover [英] Storyboard: Dismissing Popover using delegate/protocol method

查看:32
本文介绍了故事板:使用委托/协议方法关闭 Popover的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了 的内容,虽然大多数似乎是关于非故事板方法的,但我认为我已经拼凑起来并弄清楚了.但是,以下代码不会导致我的弹出框被解除.Popover 中的dismissPopoverButtonPressed 按钮执行,但委托中的dismissPopover 方法中的断点从未命中.非常感谢有人仔细查看代码以发现错误.

I've read tons of stuff on this and while most seems to be in regards to the non-storyboard approach, I thought I had pieced bits together and figured it out. However, the following code does not result in my popover being dismissed. The dismissPopoverButtonPressed button in the Popover executes but a breakpoint in the dismissPopover method in the delegate never hits. Would very much appreciate someone casting an eye over the code to spot mistakes.

谢谢

在下面,NewGameViewController 包含一个 UIButton.按下此按钮会导致 Popover Segue 并随后显示包含 PopViewController UIView 的弹出框.

In the following, NewGameViewController contains a UIButton. Pressing this results in the Popover Segue and subsequent display of the popover containing the PopViewController UIView.

NewGameViewController.h

#import "PopViewController.h"
@interface NewGameViewController: UIViewController <DismissPopoverDelegate>
{
    UIPopoverController *popover;
}

NewGameViewController.m

@implementation NewGameViewController
-(void)prepareForSegue:(UIStoryboardPopoverSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"popoverSegue"])
    {
        popover = [(UIStoryboardPopoverSegue *)segue popoverController];
        // getting warning: Assigning to 'id<UIPopoverControllerDelegate>' from incompatible type 'NewGameViewController *const__strong'
        //popover.delegate = self;
    }
}

-(void)dismissPopover
{
    [popover dismissPopoverAnimated:YES];
}

PopViewController.h

@protocol DismissPopoverDelegate <NSObject>
-(void) dismissPopover;
@end

@interface PopViewController: UIViewController
{
    __unsafe_unretained id<DismissPopoverDelegate> delegate;
}

@property (nonatomic, assign) id<DismissPopoverDelegate> delegate;
-(IBAction)dismissPopoverButtonPressed:(id)sender;
@end

PopViewController.m

#import "NewGameViewController.h"
@implementation PopViewController
@synthesize delegate;
-(IBAction)dismissPopoverButtonPressed:(id)sender
{
    [self.delegate dismissPopover];
}

推荐答案

当从 storyboard segue 链接到 popover 控制器时,segue 的 popoverController 属性引用标准的 UIPopoverController.这个控制器本身有一个属性,contentViewController,它将代表实际在弹出框中呈现的视图控制器,在您的情况下为 PopViewController.

When linking to a popover controller from a storyboard segue, the popoverController property of the segue refers to a standard UIPopoverController. This controller itself has a property, contentViewController, which will represent the view controller that is actually being presented within the popover, in your case the PopViewController.

因此,您当前的代码将自己设置为弹出框控制器的委托,而它确实需要将自己设置为弹出框 content 视图控制器的委托.

So, your current code is setting itself as the delegate of the popover controller, when it really needs to be setting itself as the delegate of the popover's content view controller.

您仍然需要保留对弹出框控制器的引用,以便关闭,因此保留现有代码,但进行以下更改:

You still need to keep a reference to the popover controller around, to dismiss, so keep your existing code, but make the following change:

-(void)prepareForSegue:(UIStoryboardPopoverSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"popoverSegue"])
    {
        popover = [(UIStoryboardPopoverSegue *)segue popoverController];
        // Get a reference to the content view controller of the popover
        PopViewController *popVC = (PopViewController*)popover.contentViewController;
        // Set ourselves as the content VC's delegate
        popVC.delegate = self;
    }
}

这篇关于故事板:使用委托/协议方法关闭 Popover的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆