【问题标题】:UIBarButtonItems cannot be removed from one toolbar and added to another?UIBarButtonItems 不能从一个工具栏中删除并添加到另一个?
【发布时间】:2012-07-22 22:17:20
【问题描述】:

我有一个 UISplitViewController。我有多个详细视图。详细视图具有工具栏。我想在工具栏之间移动从 UISplitViewControllerDelegate 提供的 UIBarButtonItem。

所以我保存了对 UIBarButtonItem 的引用,当我交换视图时,我将它从当前的详细视图中移除并将其移动到新的详细视图中。

UIBarButtonItem 只工作一次。如果我从 UISplitViewController 详细信息窗格中的视图控制器 A 开始,则该项目将显示在工具栏中。当我切换到视图控制器 B 时,我可以看到正在删除和添加的项目,它占用了空间,但它从未显示在新工具栏中。

每次我想将它添加到新工具栏时,我都可以通过复制 UIBarButtonItem 来解决这个问题。我真的很想使用保存的值。

发生了什么事?

代码

我的应用委托也是我的 UISplitViewControllerDelegate。我所有的细节视图也符合 UISplitViewControllerDelegate。

// Save
- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc
{
    /* Omitted code that calls the same method on the current detail view. */
    // Save the item
    self.savedBarButtonItem = barButtonItem;
}

我的应用委托中有一个 IBAction:

-(IBAction)changeDetailView:(id)sender
{
    /* omitted code to the the new view controller and the current view controller */
    [currentView removeBarButtonItem];

    //This adds the item but the item does not even show up.
    [newView addBarButtonItem:self.savedBarButtonItem];

    // New item with the same target and action works.
    UIBarButtonItem * newItem = 
        [[UIBarButtonItem alloc] initWithTitle:@"Test" style:UIBarButtonItemStyleBordered target:self.savedBarButtonItem.target action:self.savedBarButtonItem.action];
    [newView addBarButtonItem:newItem];

}

以及我在详细视图控制器中添加和删除 UIBarButtonItems 的方式:

-(void)addBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    NSArray * items = self.toolbar.items;
    NSMutableArray * newArr = [NSMutableArray arrayWithCapacity:[items count]+1];
    [newArr addObject:barButtonItem];
    for(NSObject * o in items)
        [newArr addObject:o];
    [self.toolbar setItems:newArr animated:YES];
}

-(void)removeBarButtonItem
{
    NSArray * items = self.toolbar.items;
    NSMutableArray * newArr = [NSMutableArray arrayWithCapacity:[items count]-1];
    for(NSInteger i=1; i<[items count]; i++)
        [newArr addObject:[items objectAtIndex:i]];
    [self.toolbar setItems:newArr animated:YES];
}

【问题讨论】:

    标签: objective-c ios uisplitviewcontroller uibarbuttonitem


    【解决方案1】:

    我怀疑问题是由于在 remove 方法中使用了动画。工具栏很可能保留对旧时代数组的引用,同时将它们动画化到视图之外。所以我建议你尝试在删除中将动画:更改为 NO,看看是否可以解决它。也许两者都试一下。如果它有效,那么这个理论是正确的。

    您从未提及此项目是否使用 customView,这可能会更有问题。

    【讨论】:

    • 是的,设置动画:没有工作!我确实尝试更改栏按钮项目上的一些视觉选项,但我认为我从未尝试过删除动画。此外,我所有的 ViewController 都是 Xcode 为您生成的模板的标准实现。
    • 我还发现您必须先删除,然后再进行添加。如果将其添加到新的详细视图中,然后将其从旧的详细视图中删除,即使使用动画仍然存在问题:NO。
    • 有趣 - 猜猜框架会保留对它的引用,即使没有动画。
    猜你喜欢
    • 2012-08-05
    • 2017-05-20
    • 1970-01-01
    • 2011-08-17
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多