rootViewController 是黑色的,不显示任何内容
rootViewController is black and shows no content
我的第一个 VC 调用 StartViewController 在启动时只是黑色(应该是黄色)。谁能帮帮我??
这是我的 AppDelegate:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
//MARK: Start VC
let navController = UINavigationController(rootViewController: StartViewController())
navController.navigationBar.isTranslucent = false
window?.rootViewController = navController
window?.isOpaque = true
return true
}
}
这是我的 StartViewController Class:
import UIKit
class StartViewController: UIViewController {
@IBOutlet weak var btn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
print("HELLO WORLD")
}
}
这是我的 Storyboard 的样子(之后添加):
感谢您的帮助!如果您需要查看其他 类 或内容,请询问! :)
您无法初始化像 StartViewController()
这样的故事板中的视图控制器。所以这不起作用:
let navController = UINavigationController(rootViewController: StartViewController())
相反,您需要使用 instantiateViewController
方法。确保还为您的视图控制器在情节提要中设置了“情节提要 ID”。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewControllerID")
let navController = UINavigationController(rootViewController: viewController)
我的第一个 VC 调用 StartViewController 在启动时只是黑色(应该是黄色)。谁能帮帮我??
这是我的 AppDelegate:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
//MARK: Start VC
let navController = UINavigationController(rootViewController: StartViewController())
navController.navigationBar.isTranslucent = false
window?.rootViewController = navController
window?.isOpaque = true
return true
}
}
这是我的 StartViewController Class:
import UIKit
class StartViewController: UIViewController {
@IBOutlet weak var btn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
print("HELLO WORLD")
}
}
这是我的 Storyboard 的样子(之后添加):
感谢您的帮助!如果您需要查看其他 类 或内容,请询问! :)
您无法初始化像 StartViewController()
这样的故事板中的视图控制器。所以这不起作用:
let navController = UINavigationController(rootViewController: StartViewController())
相反,您需要使用 instantiateViewController
方法。确保还为您的视图控制器在情节提要中设置了“情节提要 ID”。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewControllerID")
let navController = UINavigationController(rootViewController: viewController)