首页 > 解决方案 > 模拟器 iPhone X 系列 - 不断将崩溃无法识别的选择器发送到带有所有按钮的实例

问题描述

我在 Xcode 10.1 上,模拟器也是 10.1 版本,但它也发生在去年的上一个版本中。我没有打扰它,因为我正在构建基本的 ui 和后端,所以我跳过了它。现在我几乎准备好发布了,现在我需要在 X 系列上进行测试。

每当我使用5S - 8+中的任何其他常规模拟器iPhone 时,都没有问题,一切都很好,我触摸一个按钮,动作就会发生。我有一部 iPhone 7+,使用实际设备时按钮可以正常工作

但是每当我使用模拟器并选择任何iPhone X系列设备时,当我触摸任何视图控制器中的任何按钮时,我总是会崩溃

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[_UIBackButtonContainerView titleLabel]:无法识别的选择器发送到实例 0x7fdd99c759e0”

不管这是什么原因导致 X 系列出现问题[_UIBackButtonContainerView titleLabel]

我在调试模式下运行 Xcode:

在此处输入图像描述

可能是什么问题呢?

在此处输入图像描述

代码-

lazy var loginButton: UIButton = {
    let button = UIButton(type: .system)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitle("Login", for: .normal)
    button.setTitleColor(UIColor.white, for: .normal)
    button.titleLabel?.font = UIFont(name: "ArialRoundedMTBold", size: 19)
    // I also tried commenting out button.titleLabel?.font = UIFont(name: "ArialRoundedMTBold", size: 19)
    button.backgroundColor = UIColor.lightGray
    button.addTarget(self, action: #selector(loginButtonPressed), for: .touchUpInside)
    button.clipsToBounds = true
    button.layer.cornerRadius = 5
}()

@objc func loginButtonPressed() {
    // do something
}

override func viewDidLoad() {
    super.viewDidLoad()

    // there is a username textfield and a password textField above the button

    view.addSubview(loginButton)

    loginButton.topAnchor.constraint(equalTo: passwordTextField.bottomAnchor, constant: 8).isActive = true
    loginButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 16).isActive = true
    loginButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -16).isActive = true

    if UIScreen.main.bounds.width == 320 {

        loginButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
    } else {

        loginButton.heightAnchor.constraint(equalToConstant: 45).isActive = true
    }
}

这是按钮的图片,操作中只有一个打印语句:

在此处输入图像描述

这是崩溃的图片:

在此处输入图像描述

在此处输入图像描述

标签: iosswiftxcodeuibutton

解决方案


他说,我能够通过@matt 在评论中的评论缩小错误范围I'm having quite a lot of trouble understanding what any of the code you've shown could possibly have to do with UIBackButtonContainerView. It seems to me we want to be looking at your back button, not this loginButton。当他说我查看了代码的其他部分时。

这个有问题的 LoginVC 是root vc,我没有任何 barButtonItem,但是我使用下面的这行代码来隐藏backBarButtonvc 中被推送的项目中的文本,这样它只会显示一个后退箭头.

这是在推动的父vc中:

// LoginVC with loginButton inside of it
navigationItem.backBarButtonItem = UIBarButtonItem(title: nil, style: .plain, target: nil, action: nil)

这是在被推送的子 vc 中:

// child vc that’s getting pushed on
if view.frame.width == 414 && view.frame.height == 896 || view.frame.width == 375 && view.frame.height == 812  {

    navigationController?.navigationBar.prefersLargeTitles = true
} else {

    navigationController?.navigationBar.prefersLargeTitles = false
}

我不知道为什么我可以毫无问题地使用 iPhone 5、6、7 和 8 系列而不是 X 系列。当我对此发表评论时,它起作用了。

为了让它工作并仍然隐藏下一个 vc 中的文本,我不得不将代码更改为:

navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

区别在于title:参数。

当它不起作用时,我将参数设置为nil.

为了让它工作,我将它设置为空引号:""

很奇怪的问题???

在进一步检查后更新问题与

navigationController?.navigationBar.prefersLargeTitles = true

看来您不能使用上面的代码并将 backBarButton 标题设置为 nil。我猜苹果希望确保在使用大型导航栏标题时存在某种文本。


推荐阅读