按钮和一些用户交互对 iOS14 应用程序没有响应

Mat*_*199 4 html xcode ios swift xcode11

我正在为我的 iOS 应用程序的一个奇怪的错误而苦苦挣扎。在 iOS 13 应用程序中使用模拟器时,它可以正常工作,但在使用 iOS 14 按钮时,开关和其他功能没有响应。控制台没有错误输出。我不明白为什么只有 XCode 11 和 iOS 14 才会发生这种情况。

这是一个片段,在View.

 let logButton: UIButton = {
    let button = UIButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setLogInButton()
    button.setTitle(NSLocalizedString("log_in", comment: ""), for: .normal)
    return button
}()
Run Code Online (Sandbox Code Playgroud)

在这里,我将目标分配给按钮。

   override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initConstraints()
        backgroundColor = UIColor().settingsTableViewCellColor()
        logButton.addTarget(self, action: #selector(buttonLogInLogOut), for: .touchUpInside)
    }
Run Code Online (Sandbox Code Playgroud)

有行动

@objc func buttonLogInLogOut(_ sender: UIButton){
    print("Log in clicked")
    delegate?.logInLogOutFunc(sender)
}
Run Code Online (Sandbox Code Playgroud)

正如我所说,按钮(开关和其他按钮)仅在 iOS 14 中没有响应。看起来targetActions不起作用。

感谢您提供任何帮助。问候马特

Ton*_*ord 14

我在表格单元格中的按钮不起作用时遇到了同样的问题

出于某种原因,您必须将按钮添加到contentView单元格而不是单元格本身,如下所示

cell.contentView.addSubView(button)
Run Code Online (Sandbox Code Playgroud)

之后为我工作

  • 噢,伙计,你是对的。我一直认为 `contentView` 是用于执行一些操作,例如向左/向右滑动以进入编辑模式......同样奇怪的是 `addSubview` 不仅仅在 iOS 14 中工作。 (2认同)