I have 3 view controller: containerViewController, questionViewController, answerViewController.
How can I swap question and answer, without to create a new instance from the view controllers? So if one is created, use that one.
Usually I swap between them like this:
containerViewController!.performSegueWithIdentifier("question", sender:self)
but this always creates a new instance.
Yes, it always will create a new instance. have no doubt for that. Because that command equal to create a new object and push it (or present it).
there's a lot of way to walkthrough it. In my case, i usually create questionViewController with an button, and by that button, it will present answerViewController. And answerViewController will have a button to dismiss. I think you can try it.
Related
I have been searching all over the web but I can't seem to find the answer to this.
Currently i am using presentViewController to start new ViewControllers, but on certain view controllers i do not dismiss it and call over it. I currently am not using any navigation controllers or anything like that.
I am just worried that if I call the same viewController again via presentViewController, that the same viewController would have 2 running instances.
Is it possible? Or does the iOS framework automatically reuse the idle viewController?
If so, how do i remove the idle view controllers?
Thank you! (I was holding back my question and tried to find it all over the web, so if you can point me in the right direction, it would be very helpful thanks!)
iOS will not reuse your view controller, you can easily check it yourself by printing your view controller in viewDidLoad, you will notice first that viewDidLoad is called every time, and next that all objects have different addresses.
Unless you create thousand of them, or the navigation of your app doesn’t let you come back to an “idle” view controller, I would not say this is an issue though.
I don’t see any clean way to remove a view controller from the memory without calling “dismiss”. You could try to:
- “refresh” your view with new data.
- use something like UIPageViewController if the workflow of your app allows this kind of behaviour.
- rework the navigation so you can dismiss the view before calling another one
Good luck
I am creating a notecards app and I need to be able to make a new notecard as many times as I want. I want to do this so the user is not limited to an amount of note cards. I need to know how I would create a new view controller (or note card) if they want. So they creating page can be the same but the new note card cant be the same view as the last note card otherwise it wont be the new one they created. To get to the point I am trying to create a new view for a new notecard that was created.
Think of a scene in a storyboard as a template. When you invoke a view controller from a storyboard, you get a new copy.
So say your app has a master view controller with a "create note card" button on it. You could connect that button to an IBAction that instantiates and displays a new copy of your note card view controller. The IBAction method might look like this:
#IBAction func createNoteCard(UIButton sender)
{
//Create a new instance of a Note card view controller
let newCard =
self.storyboard.instantiateViewControllerWithIdentifier("NoteCard")
//Put your code to configure the view controller here.
//Display the new note card view controller to the screen
self.presentViewController(newCard, animated: true)
}
It is very common to have one design used multiple times. I think it's more logical to create one viewController, and pass it data based on which notecard is selected, or a blank dataset when a new card is "created". This is a common design for use with a UITableView (listing your notecards), which when clicked on, opens the particular card selected. The detail (notecard) view is always the same view, but you pass it different data.
I'm currently building a little sample iOS app, i developed my UIViewControllers and views programatically, i'm targeting iOS 7+ devices and i have a simple question :
Here how i show a new controller
MySuperController *superController = [[MySuperController alloc] init];
[self.navigationController showViewController:superController sender:self.navigationController];
First i wanted to know if it's the correct way to show another view controller ?
Second imagine i'm performing those instructions in a LoginViewController that will be displayed just once (typically when the user launches the app) how can i release this loginviewcontroller after creating and showing another view controller ?
i know this question has been already asked but all the solutions presented are old/inappropriate (my sample app is ARC enabled which is by default enabled i think)
I'm new to this environnement any help/indication is appreciated thank you
As Roy Nakum says in his comment, if you are using ARC, your code is fine. You create your view controller using a local strong variable, then present it. At that point the navigation controller takes ownership of it. Since your strong reference is a local variable, it does not keep ownership after your method returns.
However there is another problem with your code. This line will likely cause you problems:
MySuperController *superController = [[MySuperController alloc] init];
You should not use init to create a view controller. It won't have any contents. You should either use initWithNibName:bundle: (to load a view controller from a NIB) or instantiateViewControllerWithIdentifier (to load a view controller from a storyboard.)
It is possible to set up a view controller so its "plain" init method loads it's views, but it takes special handling in the init method, and it's not the normal way to do things.
That's a good way to present a ViewController. If you have ARC enable (default) don't worry about releasing, it will release automatically.
I'm subclassing UINavigationController and want to in order to add the ability to add previously popped view controllers back onto the stack, akin to a forward button in a web browser.
When the user presses a button, I want to add the most recently popped off view controller back onto the stack. I do this by getting the view controller at the top of my custom stack, and calling pushViewController:animated: with it.
In the case where taps on a table view cell or something to go forward a new way into the view hierarchy, I want to clear my "popped view controllers" stack. Similar to how if the user clicks on a new link in a web browser the "forward" history is cleared.
This is where my issue lies. I don't know how to differentiate between when I call pushViewController:animated: in order to restore a view controller, and when the user taps a cell to push one. In the latter case, I want to clear my stack, but in the former I don't want to.
I can't figure out what to do here. In a perfect world pushViewController:animated: would have userOptions: parameter or something on it that would allow me to distinguish between how it's being used, but unfortunately that parameter doesn't exist.
Such an issue must come up rather frequently. How would I deal with it in this case? How would I differentiate between the circumstances in which the method is being called?
If I follow you correctly one common approach to doing this is:
Your "goForward" method should call your superclass' pushViewController:animated:
Override pushViewController:animated: to call both your superclass' pushViewController:animated: and your "clearStack" method.
It seems to me that you need two different methods in your subclass. One for the case where you want to restore a view controller, and one where you want to clear the stack. Both will perform some custom logic and call pushViewController:animated: on super.
Now, I have a table view called Products with a add button on the navigation bar. When click on add button, the new view controller will be pushed to show which called AddItem. Here is the thing, Can I open a new view controllers A from AddItem and reopen Products from A?
May be my description a little bit confused.
ProductsTableViewController --(Push)--> AddItemViewController --(Modal/Push)--> AViewController --(Modal)--> ProductsTableViewController(Should be exact same as the first one)
And there may be some controllers between AddItemViewController and AViewController.
I have two solution:
1. When open ProductsTableViewController from A, get the same instance as before
2. Create a new instance of ProductsTableViewController and get data from an singleton class.
My concern is that is there any memory leak or protential risk?
your best bet is to use delegates. not sure why you want to do this, but if you have all your vcs in the stack, and your "ProductsTableViewController" is the root, then just have it move to the top of the stack.