gpt4 book ai didi

ios - UIApp 为 nil,这意味着我们无法将控制操作分派(dispatch)给它们的目标

转载 作者:行者123 更新时间:2023-11-28 13:23:43 24 4
gpt4 key购买 nike

我正在将文件从一个模块更改为另一个模块,这样做时我开始在我的一个测试中遇到此错误。早些时候它工作得非常好。

[断言] UIApp 为 nil,这意味着我们无法将控制操作分派(dispatch)给它们的目标。如果此断言被命中,我们可能在没有执行 UIApplicationMain() 的情况下到达这里,这可能意味着此代码未在应用程序中运行(可能是在没有主机应用程序的情况下运行的单元测试)并且不会按预期工作。

在 viewDidLoad() 的代码中添加按钮

    private lazy var button: ABCTypeButton = {
let button = ABCTypeButton(title: viewModel.title, buttonType: .Payment).withAutoLayout()
button.accessibilityLabel = viewModel.title
button.accessibilityIdentifier = "paymentButton"
button.resetTintColor()
button.addTarget(self, action: #selector(ABCViewController.action1), for: .touchUpInside)
button.addTarget(self, action: #selector(ABCViewController.action2), for: .touchDown)
button.addTarget(self, action: #selector(ABCViewController.action3), for: [.touchUpOutside, .touchDragExit])
return button
}()

@objc private func action1() {
// code
}

public class ABCTypeButton: UIControl {

let iconImageView = UIImageView()
let buttonTitleLabel = UILabel()
private let chevronImageView = UIImageView(image: Icon.navigateNext.image)
private let stackView = UIStackView().withAutoLayout()

public init(title buttonTitle: String,
buttonType: FeeButtonType,
height: CGFloat = Spacing.four) {
super.init(frame: CGRect.zero)
setupViews(buttonTitle, buttonType: buttonType)
setupConstraints(height: height)
}
}

尝试从测试中点击按钮。

    func test() {
let viewController = ViewController(viewModel: viewModel)
let button = viewController.view.findViewByIdentifier("paymentButton") as! ABCTypeButton
// I Checked that button is not nil
button.sendActions(for: .touchUpInside)
XCTAssertEqual(viewController.value, button.accessibilityIdentifier)
}

目标方法 action1() 没有被调用

最佳答案

我刚遇到这个,并为 touchUpInside 事件做了这个粗略的扩展。显然可以重构以接收您想要调用的任何事件。

extension UIButton {
public func touchUpInside(forTarget target: UIViewController) {
guard let action = actions(forTarget: target, forControlEvent: .touchUpInside)?.first else {
assertionFailure("could not find touchUpInside action for target")
return
}

target.perform(Selector(action))
}
}

关于ios - UIApp 为 nil,这意味着我们无法将控制操作分派(dispatch)给它们的目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58708937/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com