- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在开发一个 iOS 应用程序,它具有使用 UISearchController
为国家列表提供搜索功能的功能。直到最后一个版本,一切正常。但是现在,在应用程序中添加了一些新功能后,我遇到了一个导致应用程序崩溃的奇怪错误。每次我尝试调用 UISearchController(searchResultsController:nil)
时,我的应用程序都会崩溃,原因是 [UIView setImage:]: unrecognized selector sent to instance
。我追溯了以前发布的所有以前的提交,直到现在,但仍然没有找到罪魁祸首。我想就如何调试这个问题提出建议,因为我无法调试到 UISearchController
初始化自身。
注意 我没有提供一些代码片段,因为我觉得它没有必要,我尝试了几种方法,包括在另一个地方调用 UISearchController(searchResultsController:nil)
,删除UISearchController
的使用,删除了一些可疑的扩展......以确保 swift 没有给我另一个错误的崩溃报告
编辑 我的应用程序的目标版本是 8.0,我在 XCode 7.3.1、MacOS El Capitan 上使用 swift 2.2
EDIT 2 我尝试切换到 [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
但不幸的是,仍然因为同样的原因而崩溃。
编辑 3 来自 Crashlytics 的崩溃日志:
Fatal Exception: NSInvalidArgumentException
0 CoreFoundation 0x181ec2db0 __exceptionPreprocess
1 libobjc.A.dylib 0x181527f80 objc_exception_throw
2 CoreFoundation 0x181ec9c4c __methodDescriptionForSelector
3 CoreFoundation 0x181ec6bec ___forwarding___
4 CoreFoundation 0x181dc4c5c _CF_forwarding_prep_0
5 UIKit 0x18710f9d0 -[UISearchBar(UISearchBarStatic) _updateMagnifyingGlassView]
6 UIKit 0x18710d778 -[UISearchBar(UISearchBarStatic) _setupSearchField]
7 UIKit 0x18719d2c8 -[UISearchBar searchField]
8 UIKit 0x187114684 -[UISearchBar setPlaceholder:]
9 UIKit 0x1879c0d3c -[UISearchController _commonInit]
10 UIKit 0x1879c10cc -[UISearchController initWithSearchResultsController:]
11 EMCCountryPickerController 0x100bda6e8 -[EMCCountryPickerController loadView] (EMCCountryPickerController.m:136)
12 UIKit 0x1870208ec -[UIViewController loadViewIfRequired]
13 UIKit 0x187020844 -[UIViewController view]
14 UIKit 0x18790f450 -[_UIFullscreenPresentationController _setPresentedViewController:]
15 UIKit 0x18737cd90 -[UIPresentationController initWithPresentedViewController:presentingViewController:]
16 UIKit 0x1873a2af4 -[UIViewController _presentViewController:withAnimationController:completion:]
17 UIKit 0x1873a5890 -[UIViewController _performCoordinatedPresentOrDismiss:animated:]
18 UIKit 0x18711b3cc -[UIViewController presentViewController:animated:completion:]
19 UIKit 0x193c3084c -[UIViewControllerAccessibility presentViewController:animated:completion:]
20 bluebird-ios 0x10012e244 CountryPicker.didTapOnView() -> () (CountryPicker.swift:132)
21 bluebird-ios 0x10012e29c @objc CountryPicker.didTapOnView() -> () (CountryPicker.swift)
22 UIKit 0x1875b1f30 _UIGestureRecognizerSendTargetActions
23 UIKit 0x1871c6728 _UIGestureRecognizerSendActions
24 UIKit 0x1870537a0 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:]
25 UIKit 0x1875b3530 ___UIGestureRecognizerUpdate_block_invoke898
26 UIKit 0x1870124d8 _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks
27 UIKit 0x18700f254 _UIGestureRecognizerUpdate
28 UIKit 0x187051610 -[UIWindow _sendGesturesForEvent:]
29 UIKit 0x187050c0c -[UIWindow sendEvent:]
30 UIKit 0x18702104c -[UIApplication sendEvent:]
31 UIKit 0x193c0545c -[UIApplicationAccessibility sendEvent:]
32 UIKit 0x18701f628 _UIApplicationHandleEventQueue
33 CoreFoundation 0x181e7909c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
34 CoreFoundation 0x181e78b30 __CFRunLoopDoSources0
35 CoreFoundation 0x181e76830 __CFRunLoopRun
36 CoreFoundation 0x181da0c50 CFRunLoopRunSpecific
37 GraphicsServices 0x183688088 GSEventRunModal
38 UIKit 0x18708a088 UIApplicationMain
39 bluebird-ios 0x100225a3c main (AppDelegate.swift:13)
40 libdispatch.dylib 0x18193e8b8 (Missing)
最佳答案
问题已解决
根本原因是因为在 Initialize
上,UISearchBar
将调用 _updateMagnifyingGlassView
将放大图像设置为其 UITextField 的左 View
。在我的代码中,我有以下扩展名:
extension UITextField {
@IBInspectable var paddingLeft: CGFloat {
get {
return self.leftView?.frame.width ?? 0
} set {
let paddingView = UIView(frame: CGRectMake(0, 0, newValue, self.frame.height))
self.leftView = paddingView
self.leftViewMode = .Always
}
}
}
请注意 paddingLeft
属性不是可选属性,这将导致 UITextField
将 0
设置为此属性的默认值,从而创建一个宽度为 0 的 View ,并通过 setter 方法分配给 leftView
。这会导致 UISearchBar
在调用 _updateMagnifyingGlassView
时崩溃,如您在上面的崩溃日志中所见。
结论
使用扩展时要小心,尤其是当它会修改现有 UIKit 控件的默认行为时
关于ios - [UIView setImage :]: unrecognized selector sent to instance when init UISearchController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38749894/
自动呈现 UISearchController 和自己呈现搜索 Controller 的行为不同。 @implementation MyViewComtroller // click search b
这就是我如何设置我的 UIsearchController private func setupSearchController() { let searchResultsController
我在导航项上使用 UISearchController(导航栏已设置为使用 barTintColor 和 twinColor)。当搜索 Controller 关闭(点击搜索栏的取消按钮)时,后退按钮的
我正在尝试创建与 iOS 8 中的联系人应用程序类似的体验。其主要组件是: 保持搜索栏固定在导航栏下方 在展示时将搜索栏附加到 View 顶部(标准功能)。 然而,这说起来容易做起来难。在与 tabl
当我将 UISearchController 的搜索栏中的占位符文本更新为比搜索栏更宽的内容时,搜索图标会发生这种情况: 我通过在 viewDidLoad 中调用以下方法添加了 UISearchCon
我在学习 UISearchController通过关注 this教程。然而,UISearchBar未显示。这是我的代码: let searchController = UISearchControll
我正在使用 UISearchController 在我的表格 View 中搜索数据。我没有使用 TableView Controller 。我想在搜索栏处于事件状态时隐藏导航栏,因此我将 self.s
在我的项目中,我使用了 UITableViewController带有内部 UISearchController过滤我的 tableView 中的数据. 我过滤数据没有问题,但我需要确定我的 tabl
我目前正在将 iPad 应用程序中的所有 View Controller 迁移为使用 iOS 11 标准 searchController(在导航项上)。问题是当它作为表单在 iPad 上显示时,搜索
我在 UITableView 的标题中使用 UISearchController 的 UISearchBar。我的搜索工作正常,因为当我在搜索栏的字段中输入一些文本时,所有相关方法都会被调用,并且表格
我以编程方式创建了一个 searchBar 并将其添加到导航栏平铺 View 中。这是我的代码: UISearchController *searchController = [[UISearchC
在 UISearchDisplayController在 viewWillAppear: 中有一种方法可以在没有动画的情况下将其设置为事件状态方法,因此 View Controller 将推送已激活的
我正在向现有应用程序添加搜索栏。 我有一个表,其中填充了从服务器下载的数据,并且我正在使用新的 UISearchController。 我现在让搜索栏完全正常工作,并在用户输入搜索栏时显示一个新的过滤
我以编程方式将UISearchController添加到UIViewController中,当加载UIViewController时一切都很好,但是当我触摸UISearchBar时在UISearchC
我正在尝试发送搜索电影的请求,但是当我点击搜索栏写入文本时,我在 cellforrow 中崩溃,并且它没有调用 numberofrows 也没有调用请求。这是我到目前为止的代码: class InTh
我在 UITableViewController 上添加了一个搜索栏,但每次单击搜索栏时都会收到错误。 View 也变黑了.. 警告:尝试在 FindViewController 上呈现 OWSear
我在我的应用程序中使用 UISearchController,如下 this教程: 现在已经设置了自定义UITableViewCell。 UISearchController 弄乱了我的单元格。 看起
据我了解,UISearchController 的默认行为是: 点击搜索栏时,背景会变暗并显示“取消”按钮。 SearchResultsController 直到此时才显示。 SearchResult
我将 UISearchController searchBar 添加到 Controller 的 View 中,如下所示:self.view.addSubview(searchController.s
class PlaceSelectorViewController: UIViewController, GMSAutocompleteResultsViewControllerDelegate, U
我是一名优秀的程序员,十分优秀!