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.
Related
I intend to build a rather complicated app and I am wondering how to design it correctly.
I have a rootcontroller that is in charge of dispatching the creation of several Controllers (VC1 to VC5), each of them associated with its own storyboard.
So each of these storyboards are quite complex with several levels of controllers usually driven by a navigationcontroller.
My question is the following : let assume that the user is using the deepest controller in the storyboard of VC1. In this controller there is a button. When the user click it I want VC1 and all the controllers linked to it to close and also to have some data sent to the rootcontroller.
My idea is alse to be able to reuse VC1 to VC5 somewhere else in the app but from a different controller than rootcontroller.
What is the best design to achieve that ?
Thks
First off, there is no best design.There might be better design in this world for everything.
Your First Quest:
You can use Tabbar Controller with 5 Tabs.
Each tab will have navigation controllers for holding your complex design.
For data passing:
Unwind Segue.
Delegates.
Notifications.
For your quest :
user click it I want VC1 and all the controllers linked to it to close and also to have some data sent to the rootController.
There are many tutorials of how to go back to root view controller along with data.One of them is passing-data-between-view-controllers
I don't want to elaborate this whole procedure but the theme is.
One of many solutions:
Assign that data to destination VC' variable before moving to that VC .For navigation controller, to move to rootView controller,
[self.navigationController popToRootViewControllerAnimated:YES];
is used. Hope i've given some light. :)
For all intents and purposes, MMDrawerController basically acts like a UINavigationController, where it holds a ViewController as the root VC.
Is it ok practice to hold the MMDrawerController in a variable in the root VC or should I store a reference to the MMDrawerController in the AppDelegate?
I don't want to run into any memory issues and the reason I ask is because at some point in a Registrations Controller I have to presentViewController(rootVC). This would be a different instance of the root VC and I would either have to store the reference to the MMDrawerController in the Registrations Controller OR maybe call upon the AppDelegate and change the MMDrawerController stored there to the new one for the new Root VC?
I suppose that second option would work fine, but still curious overall about my question in the title.
Also, one last thing: Is there any way when one calls presentViewController(...) to make the new ViewController the new initial controller? As it stands, in the new presented controller I would still be able to dismissViewControllerAnimated(...) which means that the underlying stack of View Controllers are still in memory. I'd like them dumped from memory. Should I even be concerned about that?
I've got a single navigation controller that houses the primary view of the app (A dashboard of sorts) which then branches off to the other views in the app via segues etc..
There's a hamburger menu, which makes use of a Cocoa Pods sliding view controller. Basically, my question is is it possible to navigate to specific view controllers via the menu and maintain the correct "stack" going back. I'm aware you can just push to a single controller, but I'd like (if possible) to retain the view hierachy going back.
E.g.
There's a link in the menu to Page 1.3
I want to be able to push to Page 1.3
Then have the back button on that view to go to Page 1.2, then Page 1.1 etc..
I'm not sure whether this is standard practice in iOS apps, or whether it's better to adopt an Android esque approach and just add views to the stack, not worrying about trying to form the correct navigation structure when they go back through the views.
Hopefully all of that makes sense.
If you are happy to do it in code, rather than a segue, you can use the UINavigationController's setViewControllers:animated: method (see the documentation). You will need to instantiate the relevant VCs, initialise them with any relevant data/state, and then build an array with them in order from the root view controller to the top view controller:
NSArray *newStack = #[rootVC, ..., page11VC, page12VC, page13VC];
[self.navigationController setViewControllers:newStack animated:YES];
Then popping from page13VC will go to page12VC, etc, etc. You can get rootVC and any other pre-existing VCs using the viewControllers property of the navigation controller.
I meet the same problem in my project,here is my experience.
If you just want to do simple jump, you can try the three basic navigation function:
[self.navigationController popToRootViewControllerAnimated:YES]
[self.navigationController popToViewController:[[self.navigationController.viewControllers count] - mypopcount] animated:YES];
[self.navigationController popViewControllerAnimated:YES];
Bind the action with the button, you can do that.
But, if you want to do more flexible page jump in ios, you must consider:
First of all, you need to confirm that there is no direct data interact between these view controllers, which means you need coreData or other sqlite tools to store data, so the destination page can load data itself.
Secondly, you should consider the strategy, page jump means pop all viewcontroller and push a new one, or just pop or push a new one just on the navigation stack.
Good Luck!
Is it bad practice to skip view controllers in a navigation stack?
In my example I have a UINavigationController (which is being presented modally) with three UIViewControllers (firstViewController, secondViewController and thirdViewController), most of the time the user will be accessing firstViewController, maybe tapping a row which will then load secondViewController and so on, but what if in some circumstances I want to load the second or third view controllers immediately but still keep the first view controller in the stack, so that the user can still go back to the firstViewController.
I can do this but it doesn't feel right - is this something I should avoid doing?
Yes, it's called deep-linking, and it's perfectly fine. It's quite commonly done for things like coming from an external URL or push notification.
OK, bear with me. I have a storyboard based IOS app that:
Framed as a TabBar main navigation. One tab segues to a NavigationController. Fine.
In the navigation controller main view I have a CollectionController, also good.
When selecting a Collection item, I "push" (from a different storyboard) a UIViewController that works like a charm.
From the UIViewController, depending on user action, I segue to a second UIViewController
From the second UIViewController, depending on user action, I attempt to push a new instance of the second UIViewController (basically push myself even though I instantiate a new blank one from the storyboard).
When I do this, I am brought to the view controller noted in #3 above.
I'm thinking it has to do with the segue from #4 versus #4 pushing using navigation controller. Anyone?
TIA
Frank
I don't think you shouldn't do it that way. You could either use an unwind segue to go back to that controller or do popToViewController:animated: in code to go back to that controller. This will go back to the same instance of that controller which may not be what you want.
If you really need a new instance, you should create a new scene in the storyboard. You can create this new scene by copy and paste on the one you already have -- that way, it will have the same view. After you copy and paste it, you can move it to where you want, give it a new identifier, and hook up its new segue..