dekatotoro / SlideMenuControllerSwift

iOS Slide Menu View based on Google+, iQON, Feedly, Ameba iOS app. It is written in pure swift.
MIT License
3.4k stars 754 forks source link

How to push controller from side menu #289

Open sahabe1 opened 7 years ago

sahabe1 commented 7 years ago

Hi I want to push controller from side menu but its not working here is my code

let profileController = storyboard.instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileViewController self.slideMenuController()?.toggleLeft() self.navigationController?.pushViewController(profileController, animated: true)

can help me how i can push contoller

Vrezhg commented 7 years ago
let storyboard = UIStoryboard(name: "Main", bundle: nil)

let newController = storyboard.instantiateViewController(withIdentifier: "ProfileViewController")
// replace the identifier with whatever you call your menu
let leftViewController = storyboard.instantiateViewController(withIdentifier: "Left")

let slideMenuController = SlideMenuController(mainViewController: newController, leftMenuViewController: leftViewController)

// navigate to next view
self.present(slideMenuController, animated: false, completion: nil)
asciifaceman commented 7 years ago

@Vrezhg if you repeat this will those controllers pile up in memory? Genuinely curious.

Vrezhg commented 7 years ago

That's a fair question but one that I can't answer. I suppose depending on how strong of a reference the program holds to these variables. Its my understanding that as soon as there is no longer something referring to the old view controllers that it should be removed from memory. Just a theory I could be wrong

Sent from my iPhone

On Apr 16, 2017, at 12:59 AM, Charles C. notifications@github.com wrote:

@Vrezhg if you repeat this will those controllers pile up in memory? Genuinely curious.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

asciifaceman commented 7 years ago

If there was a way to do an unwind on this style of instantiation...

joyeshk commented 7 years ago

I am a newbie in both ios development and swift 3, so I don't know if my method to pushViewController from side menu is good or bad in terms of development; but it works. Here is what I did.

  1. attach a navigationController programmatically to mainViewController as shown in this repo.
  2. in leftViewController catch taps on button and save the button's desired viewController's storyboard id in userDeafaults.standards variable
  3. In mainViewController's slideMenuControllerDelegate's leftWillClose method, instantiate desired viewController from userDefaults.standard variable created in step 2. Then self.navigationController->pushViewController
OhMyApp commented 7 years ago

Use this inside your viewcontroller: self.slideMenuController()?.changeMainViewController(controller, close: true)

Full example:

class MenuViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func homePressed(sender: UIButton) {
        if let controller = self.storyboard?.instantiateViewController(withIdentifier: "HomeNav") {
            self.slideMenuController()?.changeMainViewController(controller, close: true)
        }
    }

    @IBAction func favoritesPressed(sender: UIButton) {
        if let controller = self.storyboard?.instantiateViewController(withIdentifier: "FavoritesNav") {
            self.slideMenuController()?.changeMainViewController(controller, close: true)
        }
    }
}