gpt4 book ai didi

ios - Storyboard - 弹出框的委托(delegate)

转载 作者:行者123 更新时间:2023-12-01 16:53:04 26 4
gpt4 key购买 nike

我正在使用弹出框,弹出框内部包含一个 uitableview,我想在点击 uitableview 时获得值(value),我在里面创建委托(delegate)

弹出框.h

@class PopOverVCDelegate;
@protocol PopOverVCDelegate <NSObject>

-(void)getValue:(int)value;

@end

@interface ..
{
@property(nonatomic)id<PopOverVCDelegate> delegate;
}
@end

弹出框.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.delegate getValue:[indexPath row]];
}

和 mainclass 接收委托(delegate)是

主类.h
#import "PopOverVC.h"
@interface MainClassVC : UIViewController<PopupCatchupVCDelegate,UIPopoverControllerDelegate>
{}

主类.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showPopover"])
{
[[segue destinationViewController] setDelegate:self];
UIPopoverController *p = [(UIStoryboardPopoverSegue *)segue popoverController];
p.popoverBackgroundViewClass = [CustomPopoverBackground class];
self.popoverController = p;
popoverController.delegate = self;
}
}

-(void)getValue:(int)value
{
NSLog(@"adsf-%d",value);
}

我没有从代表那里得到任何值(value)。任何人都可以帮助我,我使用 Storyboard 并从推送导航中调用弹出框。

最佳答案

在 popover.h 你声明了协议(protocol) PopOverVCDelegate & 在 mainclass.h 你正在实现 PopupCatchupVCDelegate 协议(protocol)。您的主类应该向 PopOverVCDelegate 确认。

@interface MainClassVC : UIViewController<PopOverVCDelegate, PopupCatchupVCDelegate, UIPopoverControllerDelegate>

此外,您应该在调用它之前检查委托(delegate)方法是否已实现 -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if([self.delegate respondsToSelector:@selector(getValue:)])
[self.delegate getValue:[indexPath row]];
}

编辑

我查看了 Storyboard并注意到,您的委托(delegate)没有设置为正确的 Controller 。您正在将委托(delegate)设置为 p(UIPopoverController),您应该将其设置为 popover 的 contentViewController(PopOver)。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"idenPopover"])
{
[[segue destinationViewController] setDelegate:self];
UIPopoverController *p = [(UIStoryboardPopoverSegue *)segue popoverController];
p.popoverBackgroundViewClass = [CustomPopoverBackground class];
self.popoverController = p;
self.popoverController.delegate = self;

//set delegate to PopOver
UINavigationController *navigationController = (UINavigationController*)p.contentViewController;
PopOver *popOver = (PopOver*)navigationController.topViewController;
[popOver setDelegate:self];
}
}

关于ios - Storyboard - 弹出框的委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14088108/

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