故事板 - popover代表

时间:2012-12-30 03:39:56

标签: ios storyboard popover

我正在使用popover,在popover内部包含一个uitableview,我想在点击uitableview时得到价值,我在里面创建委托

popover.h

@class PopOverVCDelegate;
@protocol PopOverVCDelegate <NSObject>

-(void)getValue:(int)value;

@end

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

popover.m

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

和mainclass接收委托是

mainclass.h

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

mainclass.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);
}

我没有从委托中获得任何价值。任何人都帮助我,我使用故事板并通过推送导航调用popover。

1 个答案:

答案 0 :(得分:0)

在popover.h中你声明了协议PopOverVCDelegate&amp;在mainclass.h中,您正在实现PopupCatchupVCDelegate协议。你的主类应该确认PopOverVCDelegate。

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

此外,您应该在调用委托方法之前检查是否实现了委托方法 -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([self.delegate respondsToSelector:@selector(getValue:)])
        [self.delegate getValue:[indexPath row]];
}

修改

我查看了故事板并注意到,你的代表没有设置正确的控制器。您正在将委托设置为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];
    }
}