appdelegate 中的导航控制器
UiNavigation controller in appdelegate
我希望我的导航控制器在我的整个应用程序中可用。是否可以在我的应用程序委托中创建一个 UI 导航控制器对象并使其在整个应用程序中可用?我可以使用此对象在整个应用程序中使用导航吗?
我在 Swift 的 app-delegate 中创建了一个对象。但它没有按预期工作。在 iPhone-simulator 中只显示导航栏,除了看导航栏我什么也做不了。背景完全变黑。
A NavigationViewController
需要一个 rootViewController
,我猜你正在 AppDelegate
中设置你的代码,如 show:
window?.rootViewController = UINavigationController(rootViewController: UIViewController())
为了解决这个问题,rootViewController
应该是一个特定的 ViewController
而不是一个虚拟的 ViewController
。
你应该创建一个 ViewController
像 FirstViewController
和内部 AppDelegate
:
window?.rootViewController = UINavigationController(rootViewController: FirstViewController())
在 AppDelegate 中的 didFinishLaunchingWithOptions 函数中,您可以添加以下代码行
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let firstViewController = storyBoard.instantiateViewController(withIdentifier: "FirstViewController")
let navController = UINavigationController()
navController.navigationBar.isHidden = true
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = navController(rootViewController: firstViewController)
window?.makeKeyAndVisible()
我不确定其他答案是否足够,所以我会投入我的 2 美分:
在您的 AppDelegate 文件中,您可以按照建议创建对 UINavigationController 的引用
@UIApplicationMain
class AppDelegate {
var window: UIWindow?
var firstViewController: FirstViewController?
var baseNavController: UINavigationController?
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
firstViewController = FirstViewController(nibName: "FirstViewController", bundle: nil)
guard let first = firstViewController else { return //you gotta handle this somehow }
baseNavController = UINavigationController(rootViewController: first)
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = baseNavController
window?.makeKeyAndVisible()
}
}
此时,您可以像这样在项目的任何位置访问您的 baseNavController
if let delegate = UIApplication.shared.delegate as? AppDelegate {
print(delegate.baseNavController)
}
我希望我的导航控制器在我的整个应用程序中可用。是否可以在我的应用程序委托中创建一个 UI 导航控制器对象并使其在整个应用程序中可用?我可以使用此对象在整个应用程序中使用导航吗?
我在 Swift 的 app-delegate 中创建了一个对象。但它没有按预期工作。在 iPhone-simulator 中只显示导航栏,除了看导航栏我什么也做不了。背景完全变黑。
A NavigationViewController
需要一个 rootViewController
,我猜你正在 AppDelegate
中设置你的代码,如 show:
window?.rootViewController = UINavigationController(rootViewController: UIViewController())
为了解决这个问题,rootViewController
应该是一个特定的 ViewController
而不是一个虚拟的 ViewController
。
你应该创建一个 ViewController
像 FirstViewController
和内部 AppDelegate
:
window?.rootViewController = UINavigationController(rootViewController: FirstViewController())
在 AppDelegate 中的 didFinishLaunchingWithOptions 函数中,您可以添加以下代码行
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let firstViewController = storyBoard.instantiateViewController(withIdentifier: "FirstViewController")
let navController = UINavigationController()
navController.navigationBar.isHidden = true
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = navController(rootViewController: firstViewController)
window?.makeKeyAndVisible()
我不确定其他答案是否足够,所以我会投入我的 2 美分:
在您的 AppDelegate 文件中,您可以按照建议创建对 UINavigationController 的引用
@UIApplicationMain
class AppDelegate {
var window: UIWindow?
var firstViewController: FirstViewController?
var baseNavController: UINavigationController?
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
firstViewController = FirstViewController(nibName: "FirstViewController", bundle: nil)
guard let first = firstViewController else { return //you gotta handle this somehow }
baseNavController = UINavigationController(rootViewController: first)
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = baseNavController
window?.makeKeyAndVisible()
}
}
此时,您可以像这样在项目的任何位置访问您的 baseNavController
if let delegate = UIApplication.shared.delegate as? AppDelegate {
print(delegate.baseNavController)
}