导航栏不见了

时间:2015-05-28 00:39:56

标签: ios swift segue appdelegate

如何从我的AppDelegate呈现一个视图控制器,并在该视图中添加一个导航栏,并带有前一个视图的后退按钮?我需要从AppDelegate以编程方式执行此操作。目前我可以从那里推出一个控制器,但它并不像一个segue。它没有添加带有后退按钮的导航栏。现在我知道我应该能够自己添加一个,但是当我这样做时它会被隐藏起来。目前我正在使用pushViewController(),但我认为这不是最佳方式。

2 个答案:

答案 0 :(得分:0)

如果你想在NavigationController中添加appDelegate,你可以这样做,这样,你的viewcontroller从故事板加载

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    let vc = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier("vc") as! ViewController
    let nav = UINavigationController(rootViewController: vc)
    self.window?.rootViewController = nav
    self.window?.backgroundColor = UIColor.whiteColor()
    self.window?.makeKeyAndVisible()
    return true
}

答案 1 :(得分:0)

我认为有些东西是相似的,如果不相同的话:

高级视图

    我的应用程序的一般组成(到目前为止,特定于手头的问题 - 注意:有关上下文提供的类的详细信息,而不是解决方案所需的)如下:
    • UIViewController(ViewController.swift)嵌入UINavigationController
    • UIViewController上的按钮会转到带有自定义类的视图:
      • ExistingLocationViewController -
          的子类
        • UITableViewController
    • UINavigationController工具栏中的其中一个按钮(添加新位置)会使用另一个自定义类进行查看:
      • NewLocationViewController -
          的子类
        • UIViewController
        • CLLocationManagerDelegate
        • UITextFieldDelegate
    • 这里还有很多其他项目,但我相信上述内容足以作为手头问题的基础

解决

    为了保持向前和向后的导航栏(和工具栏) - 我在自定义类中有以下代码(注意:以下是Swift-3代码,您可能需要调整Swift -2 ):

    override func viewDidLoad() {
        super.viewDidLoad()
        //...
        navigationController?.isNavigationBarHidden = false
        navigationController?.isToolbarHidden       = false 
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated) // #=# not sure if this is needed
        navigationController?.isNavigationBarHidden = false
        navigationController?.isToolbarHidden       = false
    }
    

    您实际上可以省略viewWillDisappear中的最后两行,或者甚至可以省略整个override功能
    最终结果(对我来说)如下所示:


希望有所帮助。