iOS-更改根视图控制器时获得黑色视图
我试图在点击按钮时将根视图控制器从ViewController1更改为ViewController2,反之亦然。问题是,在我将第二个视图控制器设置为根视图控制器后,我会出现黑屏。随信附上我的密码 我的代表,斯威夫特iOS-更改根视图控制器时获得黑色视图,ios,uiviewcontroller,rootview,Ios,Uiviewcontroller,Rootview,我试图在点击按钮时将根视图控制器从ViewController1更改为ViewController2,反之亦然。问题是,在我将第二个视图控制器设置为根视图控制器后,我会出现黑屏。随信附上我的密码 我的代表,斯威夫特 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Over
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyBoard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let vc1 = storyBoard.instantiateViewControllerWithIdentifier("VC1") as! VC1
let nc = UINavigationController(rootViewController: vc1)
self.window?.rootViewController = nc
self.window?.makeKeyAndVisible()
return true
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
let storyBoard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let vc1 = storyBoard.instantiateViewControllerWithIdentifier("VC1")
let nc = UINavigationController(rootViewController: vc1)
self.window?.rootViewController = nc
return true
}
第一视图控制器(VC1.swift)
第二视图控制器(VC2.swift)
请有人帮我找出问题所在。
提前感谢您的帮助。我会这样做: AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyBoard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let vc1 = storyBoard.instantiateViewControllerWithIdentifier("VC1") as! VC1
let nc = UINavigationController(rootViewController: vc1)
self.window?.rootViewController = nc
self.window?.makeKeyAndVisible()
return true
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
let storyBoard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let vc1 = storyBoard.instantiateViewControllerWithIdentifier("VC1")
let nc = UINavigationController(rootViewController: vc1)
self.window?.rootViewController = nc
return true
}
VC1.swift
class VC1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
deinit {
print("Deinit of VC1")
}
@IBAction func btnTapped(sender: AnyObject) {
let window = UIApplication.sharedApplication().windows[0]
let rootNavigationControl = window.rootViewController as! UINavigationController
let storyBoard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let vc2 = storyBoard.instantiateViewControllerWithIdentifier("VC2") as! VC2
let nc = UINavigationController(rootViewController: vc2)
rootNavigationControl.presentViewController(nc, animated: true) { () -> Void in
// ------- Becomes black screen here ----------
rootNavigationControl.viewControllers = [vc2]
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
class VC1: UIViewController
{
@IBAction func btnTapped(sender: AnyObject)
{
if let vc2 = self.storyboard?.instantiateViewControllerWithIdentifier("VC2")
{
self.navigationController?.pushViewController(vc2, animated: true)
}
}
}
VC2.swift
class VC2: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
deinit {
print("Deinit of VC2")
}
@IBAction func btnTapped(sender: AnyObject) {
let window = UIApplication.sharedApplication().windows[0]
let rootNavigationControl = window.rootViewController as! UINavigationController
let storyBoard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let vc1 = storyBoard.instantiateViewControllerWithIdentifier("VC1") as! VC1
let nc = UINavigationController(rootViewController: vc1)
rootNavigationControl.presentViewController(nc, animated: true) { () -> Void in
rootNavigationControl.viewControllers = [vc1]
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
class VC2: UIViewController
{
@IBAction func btnTapped(sender: AnyObject)
{
if let vc1 = self.storyboard?.instantiateViewControllerWithIdentifier("VC1")
{
self.navigationController?.pushViewController(vc1, animated: true)
}
}
}
虽然我同意在iOS中,将VC2推到导航控制器堆栈中更为常见,而且可能是更好的UX,但iOSFresh明确要求将VC1替换为VC2。并不是说VC2会被推到堆栈上。如果您真的想替换视图控制器,我会使用self.navigationController?.setViewControllers([VC2],动画:true)而不是self.navigationController?.pushViewController(VC2,动画:true)