所以我正在尝试执行以下Swift代码:
class Login: UIViewController{
...
func loginIsCorrect(){
/*problem code:*/
let main = Main(nibName: "Main", bundle: nil)
self.navigationController?.pushViewController(main, animated: true) //will not show!
/*end problem code*/
println("Hello")
}
}
我可以在控制台中看到“Hello”,但主要xib不会出现(iPhone仍然在Login.xib上)
这是我的AppDelegate.swift:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var rootViewController :UIViewController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
rootViewController = Login(nibName:"Login",bundle:nil)
window!.rootViewController = rootViewController
window!.makeKeyAndVisible()
return true
}
...
}
这是我的香草Main.swift(尚未实现):
import Foundation
import UIKit
class Main: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:1)
看起来很像你试图将主视图控制器推到不存在的导航控制器上。
另外,为了便于阅读,请重命名Login和Main LoginViewController和MainViewController。
答案 1 :(得分:0)
您尚未设置导航控制器,因此导航控制器为零。 你用过吗?在通话中所以它不会爆炸。如果你把它改成了!你的应用程序会崩溃。
self.navigationController!.pushViewController(main,animated:true)
在appdelegate中将Login视图控制器添加到UINavigationController并将其设为rootViewController
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let loginVC = Login(nibName:"Login",bundle:nil)
rootViewController = UINavigationController(rootViewController: loginVC)
window!.rootViewController = rootViewController
window!.makeKeyAndVisible()
return true
}