无法实现UIPopoverController委派

时间:2012-03-26 16:59:16

标签: iphone objective-c ipad delegates uipopovercontroller

我的UIPopover中有以下视图控制器:

@protocol StorePopoverDelegate
- (void)storeSelected:(NSString *)store;
@end

@interface StorePopoverViewController : UITableViewController
{
    NSMutableArray* items;
}

@property (nonatomic, assign) id<StorePopoverDelegate> delegate;


@end

当选择一个项目时,我希望我的代表被调用:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (delegate != nil) {
        [delegate storeSelected:[items objectAtIndex:indexPath.row]];
    }
}

实现了处理storeSelected的委托类:

-(IBAction)storesButtonClicked:(id)sender
{
    storesPopoverViewController = [[StorePopoverViewController alloc] initWithStyle:UITableViewStylePlain];
    storePopover.delegate = (id)self;
    storePopover = [[UIPopoverController alloc] initWithContentViewController:storesPopoverViewController]; 
    [storePopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

#pragma Store Popover Delegate Methods
- (void)storeSelected:(NSString *)store;
{
    NSLog(@"%@ was clicked", store);
    [storePopover dismissPopoverAnimated:YES];
}

在我的- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath中,代表是零。我不知道为什么?我正在分配它。

1 个答案:

答案 0 :(得分:1)

storesButtonClicked:方法中,您要设置storePopover的委托(此时甚至没有初始化)。您应该设置storePopoverViewController的委托属性。

storePopoverViewController.delegate = (id)self;