1

My app consists of two views. The first one is a GMSMapView and the second one is used to connect to a Bluetooth device that sends coordinates.

After the Bluetooth device is connected, I use a delegate to send the information back to the map view and move a marker around. To transition between views I was previously using segues, this didn't stop the Bluetooth view controller and the data made its way like I wished to the map view.

I ran into the problem of my map view being reinitiated so I decided to use a navigation controller. Now I use a push segue to get to my second view, and pop to come back to the same instance of the first one. Great, that worked! The issue I have now is that popping the second view seems to stop it completely from running in the background like it used to. Is there a way to keep it running in the background like it did before?

What I'm currently using to pop the second view is

self.navigationController?.popViewControllerAnimated(true)

Any idea would be appreciated! Thanks!

lhbortho
  • 167
  • 2
  • 16
  • Is there any way to have the same transition without having the view controller destroyed? Meaning, is there a way to transition from first view to second view and then back while retaining the instance of the first view? – lhbortho Apr 06 '17 at 16:46
  • Yes, and I've given an answer that tells you what it is — but I've also warned you that this is a bad idea and indicates that your app architecture is probably wrong. – matt Apr 06 '17 at 16:50
  • Also, it looks like you are using Swift 2.x. That version of Swift is now officially dead. You need to update. – matt Apr 06 '17 at 16:51
  • Thanks for the input. Swift version is correlated to what xCode version you are running right? Currently have a very old MacBook air (the only one at my work place) so I'll have to see if it can even make the jump – lhbortho Apr 06 '17 at 17:26
  • Xcode 8.3 will run only under Sierra, alas. But Xcode 8.2 will allow you to update to Swift 3, and will run on El Capitan. The jolt to your code — the differences between Swift 2 and Swift 3 — will be very dramatic and far-reaching, so the sooner you make this transformation, the better. – matt Apr 06 '17 at 17:52
  • App is only a few days old, like my knowledge of iOS and swift, I think the change won't be too bad. Thanks for the help! – lhbortho Apr 06 '17 at 17:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/140106/discussion-between-lhbortho-and-matt). – lhbortho Apr 06 '17 at 18:33

1 Answers1

4

A popped view controller does not "stop running". It is returned to you, and if you don't retain it, it is completely destroyed.

If you don't want that to happen, retain it when it is returned. You are currently ignoring the returned view controller:

 self.navigationController?.popViewControllerAnimated(true)

Instead, keep a reference to it:

self.mySecondViewController = 
    self.navigationController?.popViewControllerAnimated(true)

Be warned, however, that this is a very unusual architecture. You will not be able to use the storyboard segue to push again, because it will push a different copy. It would be better to abandon your navigation controller architecture entirely, as it is completely unsuited to the idea of a view controller persisting after it is popped. If you want an architecture where two view controllers persist simultaneously, you would be better off using a UITabBarController — or, even better, reorganize your app completely. The notion that you need the view controller to persist after being popped is a "bad smell": it means that you have put the functionality in the wrong place. Put the functionality in a place that does persist, rather than forcing the view controller to persist in some artificial way.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks for the answer Matt, I am completely new to iOS so the is exactly the kind of answer I was looking for. I will try to find another way to have my two views be persistent. Perhaps I can move my Bluetooth functionalities back to my main view controller so I do not need to have a second view controller running at the same time. – lhbortho Apr 06 '17 at 17:25
  • "Perhaps I can move my Bluetooth functionalities back to my main view controller so I do not need to have a second view controller running at the same time" Good idea! That's just the sort of thing I'm encouraging you to try doing. – matt Apr 06 '17 at 17:50
  • I previously tried doing just that but with no success. Using a UITabBarController looks like it would be easy to get working but it might not be using it the correct way. I'll give the right way a little more effort before go to a UITabBarController though. Thanks! – lhbortho Apr 06 '17 at 18:00