iOS - 更改 Root View Controller 后获取黑色 View
<p><p>我试图在按钮点击时将 Root ViewController 从 ViewController1 更改为 ViewController2,反之亦然。问题是我将第二个 ViewController 设置为 Root ViewController 后出现黑屏。我附上我的代码。</p>
<p>我的 AppDelegate.swift</p>
<pre><code>func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: ?) -> 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
}
</code></pre>
<p>第一个 ViewController(VC1.swift)</p>
<pre><code>class VC1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
deinit {
print("Deinit of VC1")
}
@IBAction func btnTapped(sender: AnyObject) {
let window = UIApplication.sharedApplication().windows
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 =
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
</code></pre>
<p>第二 ViewController(VC2.swift)</p>
<pre><code>class VC2: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
deinit {
print("Deinit of VC2")
}
@IBAction func btnTapped(sender: AnyObject) {
let window = UIApplication.sharedApplication().windows
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 =
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
</code></pre>
<p>请有人帮我找出问题所在。
提前感谢所有帮助。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我会这样做:</p>
<p>AppDelegate.swift</p>
<pre><code>func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: ?) -> 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
}
</code></pre>
<p>VC1.swift</p>
<pre><code>class VC1: UIViewController
{
@IBAction func btnTapped(sender: AnyObject)
{
if let vc2 = self.storyboard?.instantiateViewControllerWithIdentifier("VC2")
{
self.navigationController?.pushViewController(vc2, animated: true)
}
}
}
</code></pre>
<p>VC2.swift</p>
<pre><code>class VC2: UIViewController
{
@IBAction func btnTapped(sender: AnyObject)
{
if let vc1 = self.storyboard?.instantiateViewControllerWithIdentifier("VC1")
{
self.navigationController?.pushViewController(vc1, animated: true)
}
}
}
</code></pre></p>
<p style="font-size: 20px;">关于iOS - 更改 Root ViewController 后获取黑色 View ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33400696/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33400696/
</a>
</p>
页:
[1]