Reputation: 998
I'm using storyboard & segue to go from "HomeVC" to "MapVC".
HomeVC - > segue -> MapVC
And since navigation controller is embedded, MapVC automatically get back button.
Now when I'm again coming from HomeVC->MapVC, MapVC is getting reloaded. Which I don't want. Is there any way to avoid doing it programmatically.
Upvotes: 0
Views: 111
Reputation: 4096
ViewController
always loaded in stack
first in last out so when you connect any viewController
using push segue firstly it loads the whole viewController
inside the memory and when you go back to previously loaded view it will not load again so the process goes so on.
Here is atrick if you opt. You need to do just set the NSUserDefault
key true using dispatch_once
GCD block inside MapVC
viewDidLoad
method accordingly and check before it loads.
Example:
// MapVC viewController
- (void)viewDidLoad {
[super viewDidLoad];
static dispatch_once_t pred;
static id shared = nil;
dispatch_once(&pred, ^{
// set the key true here
});
if([[[NSUserDefaults standardUserDefaults] valueForKey:@"yourKey"] isEqualToString:@"true"]){
// code here which you do not wish to execute again and again
// Note: set the key false here so this block will not execute again
}
}
Note: set the key true again somewhere else if you want to execute that particular code again.
Upvotes: 0
Reputation: 52622
What do you actually want to achieve? Do you not want to display the view of MapVC?
If you stored some state in the MapVC that you are losing, then you are doing something very, very wrong. VC stands for View Controller. It controls the view. It must never, ever contain your data. So deleting one view controller and creating a new one should be absolutely painless.
Upvotes: 0
Reputation: 7591
If you're using Storyboard Segues, a new instance of your MapVC will be created every time the Segue is executed. That's just how a Segue works. To solve it you could keep a strong reference to your MapVC in the HomeVC and push that instead. The steps involved are to remove the Segue in your storyboard and assign a Storyboard Identifier on your MapVC. You probably want to create an @IBAction
to added to a button to trigger the push. Let's say you set "idMapVC"
as the identifier for your MapVC in your storyboard. You'd end up with code like this:
class HomeVC: UIViewController {
var mapVC: MapVC?
override func viewDidLoad() {
super.viewDidLoad()
mapVC = storyboard?.instantiateViewController(withIdentifier: "idMapVC") as? MapVC
}
@IBAction func pushTheMap() {
guard let mapVC = self.mapVC
else { return }
navigationController?.pushViewController(mapVC, animated: true)
}
}
Upvotes: 0
Reputation: 50129
I'd not use a segue but an IBAction and do the push manually:
e.g.
hook up a UIButton (or whatever) to trigger @IBAction func buttonClicked(sender: UIButton!)
keep a reference to mapVC as an outlet: @IBOutlet var mapVC:MyMapViewController!
[note: you could also initialize the VC manually]
implement the action to push the same mapVC
@IBAction func buttonClicked(sender: UIButton!) {
navigationController.pushViewController(mapVC, animated:true)
}
IMHO easiest / pragmatic
alternative idea: keep the segue, but connect it to a blank VC. in prepareForSegue add mapVC as a child of the segue's destinationViewController
IMO this is bad
best way I think: make the mapVC correctly persist and reload it's view based on some state you save in the homeVC.. e.g. keep visibleRegion
IMHO this is best and is how VC's are meant to be used
Upvotes: 2
Reputation: 24341
When you push MapVC
from HomeVC
, a new object of MapVC
is created each time. Its not that MapVC
is reloaded, instead a new instance is created each time.
Upvotes: 1