Code Monkey home page Code Monkey logo

Comments (9)

douglasd3 avatar douglasd3 commented on July 3, 2024

Just create an instance of your view controller on App Delegate (you can instantiate it on your view controller view did load) and instead of using a storyboard id to call the view just call the shared instance of the App Delegate. Something like this (in Swift):

        var app = UIApplication.sharedApplication()

        if let delegate = app.delegate as? AppDelegate{

            if let mainController = delegate.mainViewController{

                destViewController =  delegate.mainViewController!
            }
            else{
                destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("MainView") as UIViewController

            }
        }

from enswiftsidemenu.

sunny596 avatar sunny596 commented on July 3, 2024

@WizMeister did you have any luck with this ?
As douglasd3 mentioned, how to create and instance the view controller in App Delegate ?
And, where does the above code go ?
Sorry I am a noob. Still trying to understand how things work.
Thanks in advance !

from enswiftsidemenu.

evnaz avatar evnaz commented on July 3, 2024

You can create a singleton instance of your view controller or store your view controller in a property in the menu TVC.

from enswiftsidemenu.

douglasd3 avatar douglasd3 commented on July 3, 2024

The code goes on the table view controller of the menu, in the tableViewControllerDidSelectRow method, take a look at the example code and you will know what i'm talking about :)

To create the instance of your mainviewController in your app delegate:
In your App Delegate just add:

   //Your main view controller
   var mainViewController: UIViewController?

In your main view controller's viewDidLoad just do something like this:

   var app = UIApplication.sharedApplication()

    if let delegate = app.delegate as? AppDelegate{

        delegate.mainViewController = self
    }

You can also create a singleton like evnaz said.

Hope it helped :)

from enswiftsidemenu.

ispiropoulos avatar ispiropoulos commented on July 3, 2024

I just did it. Thanks everyone for your help! I am giving the exact guide, as the code should be written:

On your App Delegate, create an optional property for each of your view Controllers
e.g.

var firstViewController: UIViewController?

on your View Controllers ViewDidLoad method, add:

var app = UIApplication.sharedApplication()
if let delegate = app.delegate as? AppDelegate{
delegate.firstViewController = self
}

then, on the menu's tableviewcontroller, right before switch indexPath.row add:
var app = UIApplication.sharedApplication()

and then on each indexPath.row case do as such:

if let delegate = app.delegate as? AppDelegate {

            if let mainController = delegate.firstViewController {

                destViewController =  delegate.firstViewController!
            }
            else{
                destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("firstViewController") as UIViewController

            }
        } else {

        destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("firstViewController") as UIViewController
        }
        break

Be aware, that, If you have different navigation bar colors and tint colors for each view controller, you must set them in ViewWillAppear or else the colors will remain the same as the first view controller.

from enswiftsidemenu.

sunny596 avatar sunny596 commented on July 3, 2024

Hello everyone.

Thanks a lot for your help. I could follow @WizMeister code perfectly!
Though it gives an error after the same view is called back from the menu :(

Here is the error -
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported (<Test.FirstLocationViewController: 0x7bf21ae0>)

from enswiftsidemenu.

ispiropoulos avatar ispiropoulos commented on July 3, 2024

You have probably done something wrong... Did you miss any bracket? Or did you double type the pushviewcontroller on your menu's tableViewController file?

from enswiftsidemenu.

sunny596 avatar sunny596 commented on July 3, 2024

Hi @WizMeister,

One thing I can think of is that my optional property name and the view controller class name is the same.
var firstViewController: UIViewController?
and firstViewController is also the class of my view controller (of type UIViewController).

Could this be a problem ? Would it create clashes ?
I will try to change names when I get home from work and double check if I have made any duplicate calls.

from enswiftsidemenu.

ispiropoulos avatar ispiropoulos commented on July 3, 2024

No in my example all my ViewControllers are of class UIViewController, this is normal as you are subclassing UIViewController. It should work.

Kind of weird though, try making a new clean project, download the control again, to start fresh and see if there is something wrong with your current project by comparing.

from enswiftsidemenu.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.