gpt4 book ai didi

xcode - Storyboard:使用委托(delegate)/协议(protocol)方法关闭 Popover

转载 作者:行者123 更新时间:2023-12-04 02:03:07 26 4
gpt4 key购买 nike

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

谢谢

在下面,NewGameViewController 包含一个 UIButton。按下此按钮会导致 Popover Segue 并随后显示包含 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 链接到弹出框 Controller 时,popoverController segue 的属性是指标准的 UIPopoverController。这个 Controller 本身有一个属性, contentViewController ,这将表示实际在弹出窗口中呈现的 View Controller ,在您的情况下为 PopViewController .

因此,您当前的代码将自己设置为弹出框 Controller 的委托(delegate),而它确实需要将自己设置为弹出框内容 View Controller 的委托(delegate)。

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

-(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;
}
}

关于xcode - Storyboard:使用委托(delegate)/协议(protocol)方法关闭 Popover,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10966677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com