【问题标题】:[UIView setImage:]: unrecognized selector sent to instance when init UISearchController[UIView setImage:]:初始化 UISearchController 时发送到实例的无法识别的选择器
【发布时间】:2016-08-03 17:14:21
【问题描述】:

我正在开发一个 iOS 应用程序,它具有使用 UISearchController 为国家列表提供搜索功能的功能。直到最后一个版本,一切正常。但是现在,在应用程序中添加了一些新功能后,我遇到了一个导致应用程序崩溃的奇怪错误。每次我尝试调用UISearchController(searchResultsController:nil) 时,我的应用程序都会因[UIView setImage:]: unrecognized selector sent to instance 而崩溃。我追溯了从以前的版本到现在的所有以前的提交,但仍然没有找到罪魁祸首。我想就如何调试这个问题寻求建议,因为我无法调试到 UISearchController 初始化自身。

注意我没有提供一些代码sn-p因为我觉得没有必要,我尝试了几种方法包括在另一个地方调用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];,但不幸的是,由于相同的原因仍然崩溃。

EDIT 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)

【问题讨论】:

    标签: ios swift uisearchcontroller


    【解决方案1】:

    问题已解决

    根本原因是因为在Initialize 上,UISearchBar 会调用_updateMagnifyingGlassView,这会将放大图像设置为其UITextField 的左视图。在我的代码中,我有以下扩展名:

    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 属性不是可选属性,这将导致UITextField0 设置为该属性的默认值,从而创建一个宽度= 0 的视图并通过setter 方法分配给leftView .这会导致 UISearchBar 在每次调用 _updateMagnifyingGlassView 时崩溃,如您在上面的崩溃日志中所见。

    结论

    使用扩展时要小心,尤其是当它会修改现有 UIKit 控件的默认行为时

    【讨论】:

    • 最相关的答案。感谢您的解决方案。使用 UITextField 填充扩展后,应用程序在 UISearchBar 上出现崩溃。删除了扩展程序并使其正常工作。
    【解决方案2】:

    您不能以这种方式直接显示图像。为此,请遵循此 sn-p。

    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"imageName.png"]]];
    

    或者您可能绘制了imageView 的出口,但将Interface Builder 中的视图配置为UIView,而不是UIImageView。您必须将其更改为 UIImageView。崩溃与searchReslutController 无关,您的imageView 可能有误。

    【讨论】:

    • 嗨@Ishika,感谢您的回复。但不幸的是,我认为您的建议不适用于我的情况。因为我试图创建一个只有一个 UIButton 的空白视图控制器,所以该按钮挂钩到一个只做一件事的动作,即调用 UISearchController(searchResultsController:nil),它仍然导致崩溃
    • 然后尝试在viewDidLoad()中移动你的初始化代码并检查它是否有效。
    猜你喜欢
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多