我正在使用Xcode11。我删除了所有SceneDelegate方法,并通过AppDelegate设置了rootViewController。但这不会将视图控制器推到下一个。有什么问题吗?
AppDelegate类:UIResponder,UIApplicationDelegate {
可变窗口:UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow()
window?.rootViewController = ViewController()
return true
}
}
ViewController文件
让plusPhotoButton:UIButton = {
let btn = UIButton(type: .system)
btn.setImage(UIImage(named: "plus_photo"), for: .normal)
btn.addTarget(self, action: #selector(source), for: .touchUpInside)
return btn
}()
@objc func source () {
let source = SourceViewController()
self.navigationController?.pushViewController(source, animated: true)
}
答案 0 :(得分:1)
您必须将此窗口设置为keyWindow
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,didFinishLaunchingWithOptions
launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow()
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
return true
}
}
答案 1 :(得分:0)
您需要将根目录设置为navigationController
let nav = UINavigationController(rootViewController:ViewController())
nav.isNavigationBarHidden = true
window?.rootViewController = nav
为此self.navigationController?.
不为零,并推送第二个vc
答案 2 :(得分:0)
您不能删除SceneDelegate,也不需要删除它,为什么要这么做...
使用SceneDelegate可以像这样:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
self.window?.rootViewController = YourViewController()
window?.makeKeyAndVisible()
}
func sceneDidDisconnect(_ scene: UIScene) {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
}
func sceneDidBecomeActive(_ scene: UIScene) {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}
func sceneWillResignActive(_ scene: UIScene) {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
}
func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}
func sceneDidEnterBackground(_ scene: UIScene) {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
}
}
答案 3 :(得分:0)
SceneDelegate.swift
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else {
return
}
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
let mainViewController = ViewController()
let navigationController = UINavigationController(rootViewController: mainViewController)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
}