我已经阅读了一些答案,例如解雇当前的ViewController,但我的情况不同,因为我正在呈现另一个ViewController。
此代码显示视图控制器及其导航控制器,但我无法访问它的属性:
@IBAction func didTouchAddFriend(_ sender: UIButton) {
DispatchQueue.main.async {
let storyBoard = UIStoryboard(name: "CustomContact", bundle: nil)
let customContactVC = storyBoard.instantiateViewController(withIdentifier: "CustomContact")
}
}
虽然这个不起作用
@IBAction func didTouchAddFriend(_ sender: UIButton) {
DispatchQueue.main.async {
let navigation = UIStoryboard.init(name: "CustomContact", bundle: nil).instantiateInitialViewController() as! UINavigationController
let pickerUserVC = navigation.viewControllers.first as! CustomContactsViewController
self.present(pickerUserVC, animated: true, completion: nil)
}
}
我有另一个名为CustomContact.storyboard的故事板,其中包含CustomContactsViewController
当我点击didTouchAddFriend时,它只显示了这个错误:
'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <Main.MainViewController: 0x7ff29f06ac00>.'
当我打印pickerUserVC的值时,我无法理解错误cos,它显示了正确的视图控制器:
<Main.CustomContactsViewController: 0x7fd1b7922400>
MainViewController是实现didTouchAddFriend函数的当前视图控制器
答案 0 :(得分:4)
你应该展示导航控制器(它托管CustomContactsViewController
作为它的根视图控制器。)
此外,你可以跳过Dispatch调用,因为我们在一个已经在主线程中运行的@IBAction:
@IBAction func didTouchAddFriend(_ sender: UIButton) {
let navigation = UIStoryboard.init(name: "CustomContact", bundle: nil).instantiateInitialViewController() as! UINavigationController
let pickerUserVC = navigation.viewControllers.first as! CustomContactsViewController
self.present(navigation, animated: true, completion: nil)
}