11

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

谢谢

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

新游戏视图控制器.h

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

新游戏视图控制器.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];
}
4

1 回答 1

13

当从故事板 segue 链接到 popover 控制器时,segue 的popoverController属性指的是标准的 UIPopoverController。这个控制器本身有一个属性,contentViewController,它将代表实际上在弹出窗口中呈现的视图控制器,在你的例子中是PopViewController.

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

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

-(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;
    }
}
于 2012-06-10T06:41:47.903 回答