//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/