Swifter
Swifter

Reputation: 73

How to dismiss previous view controller and move to next view controller in Swift iOS

in my application i am using a UITableView as a navigation menu for my app, it has 5 cells, 'Home', 'Order Online', 'Gallery', 'Contact', 'About'.

how it works is, when the user clicks the menu button the UITableView Menu pops down and then when a cell is clicked the correct UIViewController is loaded.

Now, the problem is, the previous view controller that was presented (before i move to a new controller via the tableView menu) is not dismissed. so in the app i can present the 'order online' view controller multiple times which obviously causes a memory problem,

BUT i do not want the view controller to be dismissed when it presents the tableView as i am using a custom transition where the tableview slides down half way down the screen so a snapshot of the previous controller should still be present, only when a cell is clicked in the tableView and a new view controller is presented, should the view controller loaded before the tableView be dismissed.

here is a screenshot of storyboard :

enter image description here

here are some screenshots:

enter image description here

here is all the 'relevant' code from my TableViewController that is used as the navigation menu:

class MenuTableViewController: UITableViewController {

var menuItems = ["Home", "Order Online", "Gallery", "Contact Us", "About"]
var currentItem = "Home"


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MenuTableViewCell

    // Configure the cell...
    cell.titleLabel.text = menuItems[indexPath.row]
    cell.titleLabel.textColor = (menuItems[indexPath.row] == currentItem) ? UIColor.white : UIColor.gray

    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if (indexPath.row == 0) {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "homeView")
        self.present(controller, animated: true, completion: nil)

    }

    if (indexPath.row == 1) {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "orderView")
        self.present(controller, animated: true, completion: nil)


    }

    if (indexPath.row == 2) {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "galleryView")
        self.present(controller, animated: true, completion: nil)

    }

    if (indexPath.row == 3) {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "contactView")
        self.present(controller, animated: true, completion: nil)

    }

    if (indexPath.row == 4) {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "aboutView")
        self.present(controller, animated: true, completion: nil)

    }

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      let menuTableViewController = segue.source as! MenuTableViewController

      if menuTableViewController.tableView.indexPathForSelectedRow != nil {


      }
  }

}

i have tried using

self.dismiss(animated: true, completion: nil)

in a number of ways but it just doesn't make the app work the way i want it to, any help will be appreciated.

Upvotes: 2

Views: 5840

Answers (2)

Tonton
Tonton

Reputation: 78

In you didSelectRowAt method, you are instantiating a new instance of the view controller every time you are to present the new controllers.

You could simply instantiate them beforehand (perhaps within viewDidLoad) within your menuTableViewController, then reuse the same instances of the controllers. Doing this will also allow you to be able to dismiss them from the menuTableViewController class as it is holding references to each of the new Controllers.

Upvotes: 2

Tj3n
Tj3n

Reputation: 9943

If you are creating one, i suggest to use ONE custom navigation controller for the down menu + button, and it will control the content menu and vc, if you want to change it's root vc, just simply set vc of that custom navigation controller with self.setViewControllers([vc], animated: true), other vc will automatically dismiss itself

Upvotes: 1

Related Questions