【问题标题】:programmatic navigation back button not showing程序化导航返回按钮未显示
【发布时间】:2016-12-02 19:59:01
【问题描述】:

这是我在 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()
  }

模拟器输出:

基本上我想当用户按下返回按钮然后返回主页时。我的意思是初始页面。

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

注意:@matt 说 这是不可能的。所以你能告诉我我应该怎么做吗?我是 iOS 新手

更新:

当用户选择Man时,tabviewcontroller 的两个页面只显示关于Man的产品列表。所以如果用户想看到 Woman 然后用户回到主页选择 Woman 然后他会看到 Woman tabviewcontroller 两个页面。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    后退按钮用于返回推送到 UINavigationController 上的较早视图控制器。在您的代码中,没有返回按钮,因为没有什么可返回的; secondViewController唯一推送到 UINavigationController 上的视图控制器。

    【讨论】:

    • 你能告诉我我应该怎么做吗?我是 iOS 新手
    • 请给我建议。我能做什么
    • 我不知道你想要做什么。您在 主页/初始页面。没有什么可回头的。整个问题毫无意义。
    • 你能检查我的更新图像吗?例如:当用户选择 Man 时,tabviewcontroller 的两个页面只显示关于 Man 的产品列表。所以如果用户想看到 Woman 然后用户回到主页选择 Woman 然后他会看到 Woman tabviewcontroller 两个页面。
    • 好吧,当您的代码显示self.window!.rootViewController = nav 时,您完全放弃了男人/女人页面。你不能“回到”它,因为它甚至不再存在。它是根视图控制器,但您没有从它导航,而是将其删除。因此没有什么可回头的。
    【解决方案2】:

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

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

    编辑:看起来像这样:

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

    【讨论】:

    • 如果我理解正确,您希望在两种情况下都显示标签栏:当用户选择男人或女人时。如果该标签栏没有什么不同(它具有相同类型的数据的相同视图),那么您可以自定义其中包含的数据。下面是如何实现这一点的示例代码: override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "identifier" { let destinationController = segue.destination as? YourCustomController // 做你的任务 } }
    【解决方案3】:

    在 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)
        }
    
    }
    

    【讨论】:

    • 您可以进一步使用 FirstViewController 并在 viewDidLoad 内添加一个按钮
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 2014-01-18
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多