【问题标题】:Can't show popover controller with UIBarButtonItem无法使用 UIBarButtonItem 显示弹出框控制器
【发布时间】:2012-04-07 08:44:31
【问题描述】:

我使用故事板为我的项目开发 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];
}

【问题讨论】:

    标签: ios storyboard uipopovercontroller


    【解决方案1】:

    确保使用presentPopoverFromBarButtonItem 行上的断点按预期运行代码。此外,如果您使用 arc,请确保 myPopoverController 属性声明为 strong,否则在呈现行之前它将为 nil。

    【讨论】:

    • 是的,我正在使用 ARC,并且 popover 属性很强大。我确信代码正在命中。
    【解决方案2】:

    您是否检查过LookUpTableViewController *vc 是否确实获得了正确的实例?如果没有,请检查您的情节提要是否已将 @"lookupTable" 设置为控制器的标识符。

    【讨论】:

    • 是的,我做到了。 vc 有合适的实例
    【解决方案3】:

    代码是正确的。尝试用self.navigationItem.leftBarButtonItemself.navigationItem.rightBarButtonItem 替换发件人。发件人可能不是您所期望的。

    同时删除 if (_mode == itemSelect) 子句进行测试我不确定为什么需要直接访问 ivar _mode ivar。

    【讨论】:

    • 我使用 _mode 进行切换(编辑模式/完成按钮)。我没有导航控制器,所有 UIBarButtonItems 都放在工具栏中。发件人是我在 Interface Builder 中链接他
    • 如果它在工具栏中,你需要使用 UIPopoverArrowDirectionDown
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    相关资源
    最近更新 更多