0

enter image description here

here is my code at the 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()
  }

simulator output:

enter image description here

basicaly i want when user press back button then came back main page . i mean initial page .

i am trying with this one but it not working at least one answer for me Navigate Back to previous view controller

Note : @matt said it is imposible. so could you tell me please what should i do . i am new to iOS

Update:

when user select Man that time tabviewcontroller two page only showing list of product about Man. so if user want to see Woman then user back to main page to select Woman then he will see Woman tabviewcontroller two page.

Community
  • 1
  • 1
cristan lika
  • 415
  • 1
  • 7
  • 22

3 Answers3

0

The back button is for returning to an earlier view controller pushed onto a UINavigationController. In your code, there is no back button because there is nothing to come back to; secondViewController is the only view controller pushed onto the UINavigationController.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • could you tell me please what should i do . i am new to iOS – cristan lika Dec 02 '16 at 20:03
  • please suggest me . what can i do – cristan lika Dec 02 '16 at 20:09
  • I don't know what you _want_ to do. You are _on_ the main/initial page. There is nothing to go back to. The whole question makes no sense. – matt Dec 02 '16 at 20:17
  • could you check my update image . for example : when user select **Man** that time tabviewcontroller two page only showing list of product about **Man**. so if user want to see **Woman** then user back to main page to select **Woman** then he will see **Woman** tabviewcontroller two page . – cristan lika Dec 02 '16 at 20:41
  • Well, when your code said `self.window!.rootViewController = nav`, you completely threw away in the Man / Woman page. You cannot go "back" to it because it no longer even exists. It was the root view controller, but instead of navigating from it, you just deleted it. Thus there is nothing to go back to. – matt Dec 02 '16 at 22:34
0

Place following lines within didFinishLaunchingWithOptions in AppDelegate.swift

self.window = UIWindow(frame: UIScreen.main.bounds)
let nav = UINavigationController(rootViewController: FirstViewController())
self.window!.rootViewController = nav
self.window!.makeKeyAndVisible()

Create FirstViewController.swift with following:

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

}
AamirR
  • 11,672
  • 4
  • 59
  • 73
  • You can work even further with FirstViewController and add one more button, within `viewDidLoad` – AamirR Dec 02 '16 at 20:54
0

You should create your hierarchy like so:

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

EDIT: It will look like that: enter image description here

This way it will be possible to pop UIViewControllers contained in UITabBarController.

Adamsor
  • 730
  • 1
  • 6
  • 14
  • If I get it correct you want to show tabbar in both cases: when user selects Man or Woman. If that tabbar is no different (it have same views with same type of data) then you can just customise data contained inside. Below is example code how you can achieve that: override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "identifier" { let destinationController = segue.destination as? YourCustomController // do your task } } – Adamsor Dec 02 '16 at 21:02