使用按钮切换ViewController不会在第二个ViewController中显示NavigationBar

时间:2018-05-15 17:31:26

标签: ios swift uinavigationcontroller uinavigationbar

我想创建一个页面,如果你按下“登录”按钮(女巫位于NavigationBar,左侧项目),之后显示第二页(又名主页)的用户可以退出。同时我想记住用户使用UserDefault登录。

这是我的AppDelegate文件:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    theViewController(controllerIs: LoginPage())

    return true
}
fileprivate func theViewController(controllerIs: UIViewController) {
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()
    window?.rootViewController = UINavigationController(rootViewController: controllerIs)
}

}

登录视图:

import UIKit

class LoginPage: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .green
    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Login", style: .plain, target: self, action: #selector(handleLogin))
}
@objc private func handleLogin() {

    let root = UIApplication.shared.keyWindow?.rootViewController
    root?.present(SignOutPage(), animated: true, completion: {
        //some code here
    })


}
}

退出视图:

import UIKit

class SignOutPage: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = .yellow

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Sign Out", style: .plain, target: self, action: #selector(handleSignOut))

}
@objc private func handleSignOut() {
    let root = UIApplication.shared.keyWindow?.rootViewController
    root?.present(LoginPage(), animated: true, completion: {
        //some code here
    })

}
}

结果如下: enter image description here 和退出视图 enter image description here 代替 enter image description here

2 个答案:

答案 0 :(得分:0)

执行此操作时:

let root = UIApplication.shared.keyWindow?.rootViewController
root?.present(SignOutPage(), animated: true, completion: {
    //some code here
})

您将以模态方式呈现SignOutPage视图控制器,并将其置于根视图控制器上。

您想要做的是将其推送到导航控制器堆栈,这样您就需要这样的东西:

navigationController?.pushViewController(SignOutPage(), animated: true)

以同样的方式退出而不是呈现视图控制器时,您希望从堆栈中弹出当前的一个,执行以下操作:

navigationController?.popViewController(animated: true)

这会将应用程序返回到上一个视图控制器(登录控制器)。

答案 1 :(得分:0)

导航层次结构中有两件事情要做。

  1. 导航堆栈仅在pushViewController的情况下才会出现。如果是present模态,则需要启动新的导航堆栈

    let navVC = UINavigationController.init(rootViewController: viewController)
    self.present(navVC, animated: true, completion: nil)
    
  2. 其中,viewController是新UIViewController的对象。

    1. 每次使用导航堆栈(用于推送)和视图控制器(用于存在)时,不要使用UIApplication.shared.keyWindow?.rootViewController

      // Push
      self.navigationController?.pushViewController(viewController, animated: true)
      
      // Present Modally
      self.present(viewController, animated: true, completion: nil)
      
    2. 其中,self是您的视图控制器的对象,viewController是新UIViewController的对象。

      因此,handleLogin()上的LoginPage将如下:

      // Push
      @objc private func handleLogin() {
          guard let vc = UIStoryboard.init(name: "Main", bundle: .main).instantiateViewController(withIdentifier: "SignOutVC") as? SignOutPage else { return }
          self.navigationController?.pushViewController(vc, animated: true)
      }
      
      // Pop
      @objc private func handleLogin() {
          guard let vc = UIStoryboard.init(name: "Main", bundle: .main).instantiateViewController(withIdentifier: "SignOutVC") as? SignOutPage else { return }
          let navVC = UINavigationController.init(rootViewController: vc)
          self.present(navVC, animated: true, completion: nil)
      }
      

      并且handleSignOut()上的SignOutPage将如下:

      // Push
      @objc private func handleSignOut() {
          self.navigationController?.popViewController(animated: true)
      }
      
      // Pop
      @objc private func handleSignOut() {
          self.dismiss(animated: true, completion: nil)
      }
      

      注意:您需要将storyboard-identifier设置为:

      enter image description here