我的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
中,代表是零。我不知道为什么?我正在分配它。
答案 0 :(得分:1)
在storesButtonClicked:
方法中,您要设置storePopover
的委托(此时甚至没有初始化)。您应该设置storePopoverViewController
的委托属性。
storePopoverViewController.delegate = (id)self;