UIApp 为零,这意味着我们无法将控制操作分派到他们的目标

沙尚克·考希克

我正在将文件从一个模块更改为另一个模块,因此在我的一项测试中开始出现此错误。虽然早些时候它工作得很好。

[Assert] UIApp 为零,这意味着我们无法将控制操作分派到他们的目标。如果此断言被命中,我们可能会在没有执行 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))
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

UiApp按钮翻转setStyleAttribute

UiApp,FlexTable.removeRow()

替代已弃用的UiApp和UiInstance

UiApp-图表与 可视化API

Google应用脚本UiApp DatePicker样式

使用UiApp的动画图表

如何使用GAS在UiApp的面板上显示文件预览

Google UiApp中的Google Apps脚本验证问题

Google Apps脚本-动态添加删除UiApp表单元素

当我们说Java中Byte的宽度是8位时,这意味着什么?

将我的主人交到他们的分行

让用户将视频从我的站点服务器上传到他们的YouTube频道

为特定文件(“我们的”,“我的”,“他们的”)选择Git合并策略

根据Git,谁是“我们”,谁是“他们”?

五秒钟后自动关闭Google Apps脚本UiApp

如何为数量有限的人提供对UIApp的访问?

我们可以仅具有内联全局函数的头文件,这意味着没有.cpp文件吗?

在阅读了有关代码合同的内容后,我无法分辨他们的目标

如何允许人们将我的Facebook应用程序订阅到他们的信息页?

为什么我的ELB有两个IP地址?如何找到他们?

如何将每个用户路由/链接到他们自己的数据?

如何将仅sftp的SSH用户chroot到他们的家中?

允许用户将项目添加到他们的个人资料中

如何将新创建的用户重定向到他们的“已登录”主页?

如何将多种类型的用户重定向到他们各自的活动?

Vimeo上传访问权限,基本用户可以将视频上传到他们的帐户

如何将管理员和用户重定向到他们的页面?

Excel:自动将员工分配到他们的专业领域

可以通过流回到他们的方法被关闭?