【问题标题】:Swift 2 - Why is a UIButton not working in a view added as a child in another UIViewController?Swift 2 - 为什么 UIButton 在另一个 UIViewController 中作为子项添加的视图中不起作用?
【发布时间】:2023-03-18 08:12:01
【问题描述】:

我有一个主要的ViewController,我从第二个UiViewController 在它的视图中加载另一个视图。 第二个UiViewController 的视图加载正常,但是当我尝试在该视图中创建一个按钮时,代码不会被触发。第二个视图中的UiButton 使用touch up inside 在情节提要中连接。为什么这个按钮没有被触发?

主UiViewController:

override func viewDidLoad() {
        super.viewDidLoad()

let newsPageVC = storyboard!.instantiateViewControllerWithIdentifier("news_id") as! newsViewController

self.containerView.addSubview(newsPageVC.view)
}

第二个UiViewController

@IBAction func ClosePreviewAct(sender: AnyObject) {

        print("close")
    }

【问题讨论】:

  • 检查您的 iboutlet 连接
  • 我做到了,它们连接得很好。我已将按钮拖到第二个视图控制器并创建了一个 @IBAction

标签: ios uiviewcontroller uibutton swift2


【解决方案1】:

swift 3.0 的版本

self.addChildViewController(newsPageVC)
newsPageVC.didMove(toParentViewController: self)

【讨论】:

    【解决方案2】:

    现在一切正常。 代码丢失了

    self.addChildViewController(newsPageVC)
            newsPageVC.didMoveToParentViewController(self)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多