Reputation: 1435
So I have an app that where you choose a button in the first screen it will bring you to another screen depending on which one you chose on the first one. Because I have these possible different layouts I can't just use a segue from one button to the next view controller because there is a middle screen involved. SO it is like this:
1 ---> 2 ---> 3a || 3b || 3c || 3d etc...
So your choice on the first screen dictates what your third screen will look like.
So instead I was thinking to have a value associated with each button in the first view, have it sent to the second view via a segue, and then sent again to the third view and tell it to use the proper segue to the proper third view Controller based on the value associated with the first view. But I can't do that because I can't set the second screen to segue to all the possible layouts for the third screen...At least i think I can't.
Bottom line question: is there anyway to go to another view controller after clicking a button without a segue while also sending data to that next view controller, again, without a segue to store it in?
Please help mehhh
Upvotes: 2
Views: 2652
Reputation: 154621
So instead I was thinking to have a value associated with each button in the first view, have it sent to the second view via a segue, and then sent again to the third view and tell it to use the proper segue to the proper third view Controller based on the value associated with the first view. But I can't do that because I can't set the second screen to segue to all the possible layouts for the third screen...At least i think I can't.
You can do this with segues; you just have to wire them up properly. Instead of hooking the segues up to buttons, you can create them by control-dragging from the ViewController icon at the top to the next ViewController.
Once you have created such a segue, click on the resulting segue arrow and give it an identifier in the Attributes Inspector (something like "segueToVC3a").
So, you would do this multiple times for each segue from VC2 to VC3a, VC2 to VC3b, etc.
Assuming in your first viewController you decide the value of whereToGo
and you pass that to your second viewController with a segue. Then in your second viewController, you could then do something like this:
var identifier: String = ""
switch whereToGo {
case "VC3a": identifier = "segueToVC3a"
case "VC3b": identifier = "segueToVC3b"
default: break
}
if identifier != "" {
performSegueWithIdentifer(identifier, sender: self)
}
Upvotes: 2
Reputation: 3680
Follow these steps...
1: Create a Separate file called Manager.swift and place this code in it...
//manager.swift
import Foundation
struct Manager {
static var messageText = String()
}
2: Clean your project by pressing Shift+Command+K.
3: In the first view controller set the messageText to the data you want to pass...
Manager.messageText = self.dataToPass
4: In the second view controller retrieve the data and set the content to the message text...
self.dataToReceive = Manager.messageText
5: Your Finished!!
Upvotes: 3