【发布时间】:2019-05-31 17:20:38
【问题描述】:
尝试构建我的第一个应用程序。使用 TableView 呈现 4 个不同的部分。每个部分都有自己的单元格类型,在 .xib 文件中定义。在 TableView 的最后一部分,一个单元格显示有两个按钮,加号和减号。
例如,当按下加号按钮时,它会调用与单元格相关的类中的以下代码:
// link to the viewcontroller with the tableview in it
let mainViewController = ViewController()
// func called when plusbutton is tapped
@IBAction func plusButtonTapped(_ sender: UIButton) {
// viewcontroller that should popup when the button is tapped
let popup = PopupViewController()
let sbPopup = SBCardPopupViewController(contentViewController: popup)
// call to show()
sbPopup.show(onViewController: mainViewController)
}
// show() function that is called
public func show(onViewController viewController: UIViewController) {
self.modalPresentationStyle = .overCurrentContext
viewController.present(self, animated: false, completion: nil)
}
当我在模拟器中点击按钮时,它应该会显示 PopupViewController,但会显示以下警告:
Warning: Attempt to present "balance1.SBCardPopupViewController: 0x7fdf8752cb20> on <balance1.ViewController: 0x7fdf8743cac0> whose view is not in the window hierarchy!
我尝试研究该警告并发现了一些与之相关的其他主题。没有一个解决方案对我有用。也许这与我从 .xib 文件中定义的单元格调用 plusButtonTapped() 的事实有关?
我在 youtube 上关注了这个教程:https://www.youtube.com/watch?v=9yUIOjykPDU 一切都很顺利,直到 10:36,show() 在调用函数的按钮所在的同一 VC 上被调用。在我的例子中,调用 show() 函数的 plusButton 位于一个单独的 .xib 文件中定义的单元格上,因此不在(主)VC 上(如教程中所示)。这与我面临的问题有什么关系吗?
【问题讨论】:
标签: ios swift uitableview uiviewcontroller xib