【问题标题】:How to pushViewController from a custom UIView in Swift?如何从 Swift 中的自定义 UIView 推送 ViewController?
【发布时间】:2020-10-15 11:12:57
【问题描述】:

我有一个自定义 UIView,它会在用户转到特定 UIViewController 时显示,在这个 UIView 上有一个按钮。

当用户点击按钮时,我想推送一个视图控制器但我不能调用self.navigationController?.pushViewController(inputCode, animated: true)

如何从自定义 UIView 执行相同的行为?而且我没有使用故事板。

【问题讨论】:

  • 是的...您需要为此使用delegateclosure

标签: swift uiview uiviewcontroller


【解决方案1】:

除非您传递它的引用,否则您无法从自定义 UIView 访问 UIViewController。无论如何,如果你这样做,那就不好了。您可以通过从 UIViewController 端添加操作来处理它

yourCustomView.yourActionButton.addTaget(self, action: #selector(self.tappedYourActionButton), forControlEvents: .touchUpInside)

@objc func tappedYourActionButton(_ sender: UIButton?) {

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多