程序化导航后退按钮不显示

时间:2016-12-02 19:59:01

标签: ios swift

enter image description here

这是我在appdelegate的代码:

  func showMainView()
  {
    self.window = UIWindow(frame: UIScreen.main.bounds)
    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let secondViewController = storyBoard.instantiateViewController(withIdentifier: "PRODUCT_TABBAR_VC_ID")
    let nav = UINavigationController()
    nav.pushViewController(secondViewController, animated: true)
    self.window!.rootViewController = nav
    self.window!.makeKeyAndVisible()
  }

模拟器输出:

enter image description here

基本我想要当用户按下后退按钮然后回到主页面。我是指初始页面。

我正在尝试这个,但它至少没有给我一个答案Navigate Back to previous view controller

注意:@matt说这是不可能的。那你能告诉我,我该怎么办。我是iOS的新手

更新

当用户选择时,tabviewcontroller两页仅显示关于男人的产品列表。因此,如果用户希望看到女性,则用户返回主页以选择女性,然后他会看到女性 tabviewcontroller两页。

3 个答案:

答案 0 :(得分:0)

后退按钮用于返回推送到UINavigationController的早期视图控制器。在你的代码中,没有后退按钮,因为没有任何东西可以回复; secondViewController是推送到UINavigationController的唯一视图控制器。

答案 1 :(得分:0)

在AppDelegate.swift

中的didFinishLaunchingWithOptions内放置以下行
self.window = UIWindow(frame: UIScreen.main.bounds)
let nav = UINavigationController(rootViewController: FirstViewController())
self.window!.rootViewController = nav
self.window!.makeKeyAndVisible()

使用以下命令创建FirstViewController.swift:

class FirstViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let button1 = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
        button1.setTitle("Man", for: .normal)
        button1.tag = 1
        self.view.addSubview(button1)
        button1.addTarget(self, action: #selector(showAction(sender:)), for: .touchUpInside)

        let button2 = UIButton(frame: CGRect(x: 0, y: 250, width: 200, height: 200))
        button2.setTitle("Woman", for: .normal)
        button2.tag = 2
        self.view.addSubview(button2)
        button2.addTarget(self, action: #selector(showAction(sender:)), for: .touchUpInside)

    }

    func showAction(sender: UIButton) {
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let secondViewController = storyBoard.instantiateViewController(withIdentifier: "PRODUCT_TABBAR_VC_ID")
        if (sender.tag == 1) {
            // SHOW MAN
        } else if (sender.tag == 2) {
            // SHOW WOMAN
        }
        self.navigationController?.pushViewController(secondViewController, animated: true)
    }

}

答案 2 :(得分:0)

您应该像这样创建层次结构:

                                                                             ----> UIViewController
----> UINavigationController ----> UIViewController ----> UITabBarController
                                                                             ----> UIViewController

编辑:看起来像这样:enter image description here

这样就可以弹出UITabBarController中包含的UIViewControllers。