Swift 4 - 如何从当前视图控制器设置和显示新的根视图控制器
Swift 4 - How to set and show a new root view controller from current view controller
为此苦苦挣扎了几天...
我已经在 didFinishLaunchingWithOptions
中从 AppDelegate
设置了一个 rootVC
,这工作正常。
现在 rootVC
我想要那个,如果满足某些条件,例如。 if x=y
设置并显示了一个新的 rootVC
。
我的堆栈溢出了很长时间,找到了不同的解决方案,但是 none 正在工作。
下面是编译,执行,断点检查,app里什么也没有
animated Bool false
self Regional2.IrelandViewController 0x0000000102b0ff30
newViewController Regional2.IrelandMain 0x0000000102d0d300
customViewControllersArray NSArray 0x00000001c000b620
ObjectiveC.NSObject NSObject
Exception State Registers
far unsigned long 0x00000001b0a7ba78
esr unsigned int 0x92000007
exception unsigned int 0x00000000
Floating Point Registers
General Purpose Registers
和这段代码。 . .
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let newViewController = self.storyboard?.instantiateViewController(withIdentifier: "IrelandMain") as! IrelandMain
let customViewControllersArray : NSArray = [newViewController]
self.navigationController?.viewControllers = customViewControllersArray as! [UIViewController]
self.navigationController?.pushViewController(newViewController, animated: true)
}
请注意 navigationController
show 产生相同的输出。没有任何变化。
该代码对我有用。
它只是替换了整个 UIWindow。
当我试图只替换 rootViewController 时,它成功了,但它创建了另一个(所以我在视图层次结构中有两个加载的 viewController)
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
// If we only reloads the rootViewController than the main window has 2 subviews
// The older cannot be removed and the new can be removed.
// The workaround I found is to replace the whole UIWindow with new one
let root = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateInitialViewController()
let newWindow = UIWindow()
appDelegate.replaceWindow(newWindow)
newWindow.rootViewController = root
}
并在您的 AppDelegate 中
func replaceWindow(_ newWindow: UIWindow) {
if let oldWindow = window {
newWindow.frame = oldWindow.frame
newWindow.windowLevel = oldWindow.windowLevel
newWindow.screen = oldWindow.screen
newWindow.isHidden = false
window = newWindow
oldWindow.removeFromSuperview()
}
}
为此苦苦挣扎了几天...
我已经在 didFinishLaunchingWithOptions
中从 AppDelegate
设置了一个 rootVC
,这工作正常。
现在 rootVC
我想要那个,如果满足某些条件,例如。 if x=y
设置并显示了一个新的 rootVC
。
我的堆栈溢出了很长时间,找到了不同的解决方案,但是 none 正在工作。
下面是编译,执行,断点检查,app里什么也没有
animated Bool false
self Regional2.IrelandViewController 0x0000000102b0ff30
newViewController Regional2.IrelandMain 0x0000000102d0d300
customViewControllersArray NSArray 0x00000001c000b620
ObjectiveC.NSObject NSObject
Exception State Registers
far unsigned long 0x00000001b0a7ba78
esr unsigned int 0x92000007
exception unsigned int 0x00000000
Floating Point Registers
General Purpose Registers
和这段代码。 . .
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let newViewController = self.storyboard?.instantiateViewController(withIdentifier: "IrelandMain") as! IrelandMain
let customViewControllersArray : NSArray = [newViewController]
self.navigationController?.viewControllers = customViewControllersArray as! [UIViewController]
self.navigationController?.pushViewController(newViewController, animated: true)
}
请注意 navigationController
show 产生相同的输出。没有任何变化。
该代码对我有用。 它只是替换了整个 UIWindow。
当我试图只替换 rootViewController 时,它成功了,但它创建了另一个(所以我在视图层次结构中有两个加载的 viewController)
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
// If we only reloads the rootViewController than the main window has 2 subviews
// The older cannot be removed and the new can be removed.
// The workaround I found is to replace the whole UIWindow with new one
let root = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateInitialViewController()
let newWindow = UIWindow()
appDelegate.replaceWindow(newWindow)
newWindow.rootViewController = root
}
并在您的 AppDelegate 中
func replaceWindow(_ newWindow: UIWindow) {
if let oldWindow = window {
newWindow.frame = oldWindow.frame
newWindow.windowLevel = oldWindow.windowLevel
newWindow.screen = oldWindow.screen
newWindow.isHidden = false
window = newWindow
oldWindow.removeFromSuperview()
}
}