Ios 调用SetRightBarButtonims时崩溃:动画:多次,为什么?

Ios 调用SetRightBarButtonims时崩溃:动画:多次,为什么?,ios,objective-c,cocoa-touch,uinavigationbar,uipopovercontroller,Ios,Objective C,Cocoa Touch,Uinavigationbar,Uipopovercontroller,我有两种方法可以切换视图控制器的UINavigationItem中的右栏按钮项。它基本上是在点击搜索按钮时在导航栏中显示搜索栏,在用户执行搜索时隐藏搜索栏 - (void)displaySearchUI { [self.mainNavigationController.topViewController.navigationItem setRightBarButtonItems:[self searchOpenItems] animated:YES]; [self.searchB

我有两种方法可以切换视图控制器的UINavigationItem中的右栏按钮项。它基本上是在点击搜索按钮时在导航栏中显示搜索栏,在用户执行搜索时隐藏搜索栏

- (void)displaySearchUI
{
    [self.mainNavigationController.topViewController.navigationItem setRightBarButtonItems:[self searchOpenItems] animated:YES];
    [self.searchBar becomeFirstResponder];
}

- (void)dismissSearchUI
{
    [self.mainNavigationController.topViewController.navigationItem setRightBarButtonItems:[self searchClosedItems] animated:YES];
}

- (NSArray *)searchClosedItems
{
    return @[[self someCustomItem],
             [self someOtherItem],
             [self searchButtonItem],
             ];
}

- (NSArray *)searchOpenItems
{
    return @[[self someCustomItem],
             [self someOtherItem],
             [self searchBarItem],
             ];
}
从searchButtonItem返回的项使用带有图像的UIBarButtonItem,而从searchBarItem返回的项具有UISearchBar的自定义视图。每个搜索状态中的其他项目都是相同的

当我调用displaySearchUI时,作为第一响应者的搜索栏会打开一个弹出窗口,显示一些搜索建议。当用户解除popover时,通过PopOvercontrollerdDismissPopover:delegate方法调用dismissSearchUI。但是,在这一点上,应用程序经常会在UINavigationBar中遇到崩溃:

-[CALayer enumerateObjectsUsingBlock:]: unrecognized selector sent to instance 0x7de9ac80
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayer enumerateObjectsUsingBlock:]: unrecognized selector sent to instance 0x7de9ac80'
*** First throw call stack:
(
    0   CoreFoundation                      0x056f0946 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x05379a97 objc_exception_throw + 44
    2   CoreFoundation                      0x056f85c5 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
    3   CoreFoundation                      0x056413e7 ___forwarding___ + 1047
    4   CoreFoundation                      0x05640fae _CF_forwarding_prep_0 + 14
    5   UIKit                               0x03ba38aa -[UINavigationBar _setLeftViews:rightViews:] + 3774
    6   UIKit                               0x03b8f77e -[UINavigationItem updateNavigationBarButtonsAnimated:] + 186
    7   UIKit                               0x03b901a0 -[UINavigationItem setObject:forLeftRightKeyPath:animated:] + 600
    8   UIKit                               0x03b9100a -[UINavigationItem setRightBarButtonItems:animated:] + 104
    9   MyApp                               0x0028088b -[DisplayManager dismissSearchUI] + 283
我不确定这里到底会发生什么。无论设置项目是否设置动画,似乎都会发生这种情况。它也只在用户点击popover外部时发生。如果我以编程方式关闭popover并调用dismissSearchUI,则不会出现崩溃(我认为这是由于动画发生的顺序不同,或者是由于用户关闭popover与程序性关闭时UI中的某些状态差异造成的)。我尝试通过这样做来解决这一问题:

#pragma mark - UIPopoverControllerDelegate

- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
{
    [self dismissSearchSuggestionsUI];
    return NO;
}

- (void)dismissSearchSuggestionsUI
{
    [self.popoverController dismissPopoverAnimated:YES];
    [self.displayManager dismissSearchUI];
}
但我在这里得到了一个EXC_BAD_访问崩溃(类似的堆栈跟踪使我相信这是同一个问题,只是由于时间原因出现了不同的症状)


有人遇到过类似的情况吗?有没有一种方法可以解决这个问题,而不必完全禁用手动用户popover解聘功能?

Bah,我在发布后就发现了这一点。通过使用ammending DismissearchUI的方法:

- (void)dismissSearchUI
{
    [self.searchBar resignFirstResponder]
    [self.mainNavigationController.topViewController.navigationItem setRightBarButtonItems:[self searchClosedItems] animated:YES];
}
我解决了这个问题。我认为当用户执行搜索时,搜索栏会退出自己的第一响应程序,但当用户手动取消搜索时,不会自动发生这种情况,这会导致导航栏中的栏按钮项被删除