ios - EXC_BAD_ACCESS 与 UIBarButtonItem appearanceWhenContainedIn 在 iOS 7

标签 ios ios7 uisearchbar exc-bad-access

//Set all cancel buttons in search bars to "Done"
id searchBarButton = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {
    [searchBarButton setTitle:@"Done"];
} else {
    //Can't do anything here or i get EXC_BAD_ACCESS
}

这在 viewDidLoad 中调用时给出 EXC_BAD_ACCESS仅适用于 iOS 7 Gold Master 及更高版本。 iOS 7 beta 6 及更早版本都可以。

在 iOS 7 中是否有不同的方法来执行此操作?

NSLog("%@", searchBarButton)在 iOS7 上的结果是:

2013-10-01 16:14:25.972 MP Staging[12293:a0b] <_UIBarItemAppearance:0x1aaf72d0> <Customizable class: UIBarButtonItem> when contained in ( UISearchBar ) with invocations (null)>

和这个在 iOS 6 上

<_UIBarItemAppearance: 0x1c671aa0>

最佳答案

setTitle 在 iOS7 中会失败。

尝试以下来自 this 的代码博客:

-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
    self.searchDisplayController.searchBar.showsCancelButton = YES;
    UIButton *cancelButton;
    UIView *topView = self.searchDisplayController.searchBar.subviews[0];
    for (UIView *subView in topView.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
            cancelButton = (UIButton*)subView;
        }
    }
    if (cancelButton) {
      //Set the new title of the cancel button
        [cancelButton setTitle:@"Annuller" forState:UIControlStateNormal];
    }
}

关于ios - EXC_BAD_ACCESS 与 UIBarButtonItem appearanceWhenContainedIn 在 iOS 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19124516/

相关文章:

ios - 具有相同变量值的多个自动布局约束

iphone - iOS 应用程序共享文档目录

ios appearanceWhenContainedIn 只工作一次

ios - 如何更改显示在 UISearchBar +iPhone 中的取消按钮的默认文本

iPhone SDK : UISearchBar: searchBarTextDidEndEditing not firing

嵌套在 View 中的 iOS 按钮未接收到触摸

ios - 如何到达发件人的父 View ?

ios - 如何以编程方式检查字符串的日期格式是否正确

iphone - 在 iOS7 中处理一个 View Controller 的自动旋转

ios - 无法在 iOS8 上的 iTunes Store 中搜索