我想在两个View Controller之间进行更改。两个ViewControllers都继承自CenterViewController。下面的代码是将一个ViewController(CertificatenViewController)添加到堆栈中:
centerViewController = CertificatenViewController(nibName: "CertificatenViewController", bundle: nil)
centerViewController.delegate = self
centerNavigationController = UINavigationController(rootViewController: centerViewController)
view.addSubview(centerNavigationController.view)
addChildViewController(centerNavigationController)
centerNavigationController.didMoveToParentViewController(self)
这发生在viewDidLoad和同一个类中我有一个switch语句来检查哪个View必须加载,如下所示:
switch menuItem.getNibname() {
case "CertificatenViewController":
print(menuItem.getNibname())
centerViewController = CertificatenViewController(nibName: menuItem.getNibname(), bundle: nil)
centerViewController.delegate = self
case "SettingsViewController":
print(menuItem.getNibname())
centerViewController = SettingsViewController(nibName: menuItem.getNibname(), bundle: nil)
centerViewController.delegate = self
default: break
}
我不知道究竟是什么问题,但似乎没有重新加载视图。我试过popViewControllerAnimated,但这不起作用。也许你可以帮我解决这个问题。
解决方案:
func changeView(menuItem: MenuItem){
self.centerNavigationController.viewControllers.removeAll()
switch menuItem.getNibname() {
case "CertificatenViewController":
print(menuItem.getNibname())
self.centerNavigationController.pushViewController(self.centerViewController, animated: false)
case "SettingsViewController":
print(menuItem.getNibname())
self.centerNavigationController.pushViewController(self.settingsViewController, animated: false)
case "MessagesViewController":
print(menuItem.getNibname())
self.centerNavigationController.pushViewController(self.messagesViewController, animated: false)
default: break
}
}
答案 0 :(得分:2)
通常,这不是呈现视图控制器的方式:
view.addSubview(centerNavigationController.view)
addChildViewController(centerNavigationController)
centerNavigationController.didMoveToParentViewController(self)
只需删除该代码即可。
我不确定你到底想要做什么,但是像
那样self.presentViewController(centerNavigationController, animated: true, complettion: nil)
是典型的。这使得当前视图控制器显示导航控制器(通常仅在其自身之上)。然后导航控制器将显示您自己设置的根(所以centerViewController)。
如果要删除它,请在执行演示的视图控制器实例上调用dismissViewControllerAnimated:completion:
。它取消了导航控制器(不要在导航控制器上调用dismiss)
稍后,当您要替换时,执行此操作:
centerViewController = CertificatenViewController(nibName: menuItem.getNibname(), bundle: nil)
centerViewController.delegate = self
添加:
centerNavigationController.setViewControllers([centerViewController], animated: true)