ios – 无法使用UIBarButtonItem显示弹出控制器

我使用storyboard为我的项目开发UI.有很多问题都解决了,但这个问题让我很难过.我为UIBarButtonItem添加了操作:

- (IBAction)pressAddActionButton:(UIBarButtonItem *)sender {
if (_mode == itemSelect) {

    LookUpTableViewController *vc =  [self.storyboard instantiateViewControllerWithIdentifier:@"lookupTable"];
    vc.key = @"title";
    vc.data = [Linesheet MR_findAllSortedBy:@"title" ascending:YES];
    vc.lookUpDelegate = self;

    self.myPopoverController = [[UIPopoverController alloc] initWithContentViewController:vc];
    [self.myPopoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];      
} else {
    self.mode = itemSelect;
}

}

如果我使用storyBoard segue显示弹出窗口 – 一切都很好,但如果我在运行时这样做,则不显示弹出窗口.我应该手动创建UIBarButtonItem.

感谢帮助!!!

更新,按钮代码:

- (void)setupNavigationItems {

    self.navigationController.navigationBarHidden = NO;

    UIBarButtonItem *addItem;
    if (_mode == itemSelect) {
        addItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                                                                target:self 
                                                                action:@selector(pressAddActionButton:)];   
    } else {
        addItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone 
                                                  target:self 
                                                  action:@selector(pressDoneButton:)];
    }
    [addItem setStyle:UIBarButtonItemStyleBordered];

    UIBarButtonItem *separator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                                               target:self 
                                                                               action:nil];

    UIBarButtonItem *action = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction 
                                                                            target:self 
                                                                            action:@selector(pressActionButton:)];
    [action setStyle:UIBarButtonItemStyleBordered];

    [toolbar setItems:[NSArray arrayWithObjects:separator, addItem, action, nil] animated:YES];
}

最佳答案 确保使用presentPopoverFromBarButtonItem行上的断点按预期命中代码.此外,如果您正在使用arc,请确保将myPopoverController属性声明为strong,否则在呈现行之前将为nil.

点赞