0

I try to perform a segue programatically from one view to another, way after it's been loaded (server sends an order to proceed) but it won't happen because "whose view is not in view hierarchy". Any ideas on how to solve it?

P.S. I don't perform any segues from viewDidLoad() method, all of them are performed from a method which is also called from another method in this class (which reads a dictionary, sent from the server). Also I tried to perform segues like this

dispatch_async(dispatch_get_main_queue(), {
     self.performSegueWithIdentifier("seg_wait_ready", sender: self)
})

and just like this (even though I know this is wrong):

performSegueWithIdentifier("seg_wait_ready", sender: self)

None of these work, I still get the same warning.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
EBDOKUM
  • 1,696
  • 3
  • 15
  • 33
  • Double check the storyboard - make sure "self" in this case is the VC you think it is on the storyboard, and that you have a segue with that ID connected to that ID. – inorganik Dec 16 '15 at 17:00
  • It works perfectly well if I run this application with two devices in the MCSession (I am using an MultipeerConnectivity framework), but as soon as I add another one, things get messy – EBDOKUM Dec 16 '15 at 17:03
  • You should explain what the MultipeerConnectivity framework is in your question since it clearly is part of the problem. – inorganik Dec 16 '15 at 17:25
  • All it does in this case is that server(nominal) sends a dictionary with "type": "launch" pair, which, after read by clients(also nominal), calls the `proceed()` method, which contents are described above. – EBDOKUM Dec 16 '15 at 17:29
  • Ok well how bout this part "if I run this application with two devices in the MCSession... but as soon as I add another one, things get messy"? – inorganik Dec 16 '15 at 17:47
  • @inorganik Ok, here is how it works: Server starts advertising itself and connecting other, browsing peers. After that (on button press), server sends a dictionary to all clients and proceeds to another ViewController. Clients, after they get the dictionary, check its type with help of `handleData(data: [String: String])` method, implemented from custom protocol, called from `MPCHandler` class' "session(session: MCSession, didReceiveData data: NSData, fromPeer peerID: MCPeerID)" method. If `data["type"] == "launcher"` -> `proceed()`. – EBDOKUM Dec 16 '15 at 17:58

1 Answers1

0

I solved this issue by changing the logic of how my application changes VCs. Now I do it directly through the delegate by doing the following (roughly):

delegate.window!.rootViewController = destinationViewController

It cost me all the system transition animations but I like to do custom ones anyway. But remember to keep it in main queue. To do it safe put it in the main queue manually like this:

dispatch_async(dispatch_get_main_queue(), {delegate.window! ... })

EBDOKUM
  • 1,696
  • 3
  • 15
  • 33