Passing data between two View Controllers which aren't connected by a segue [duplicate] - ios

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 7 years ago.
I know that you can pass information between two view controllers if they are connected by a segue using
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
guard let destinationViewController = segue.destinationViewController as? searchTermViewController else { return }
destinationViewController.courseCodes = selectedCourses
}
}
The above code gives an error if there is no segue because of the .destinationViewController. How do i pass information between to arbitrary view controllers without having to set up a global variable?

You can set up a delegate pattern in order to do this.
Here are the steps for setting up the delegate pattern between two objects, where object A is the delegate for object B, and object B will send messages back to A. The steps are:
Define a delegate protocol for object B.
Give object B an optional delegate variable. This variable should be weak.
Make object B send messages to its delegate when something interesting happens, such as when it needs a piece of information. You write delegate?.methodName(self, . . .)
Make object A conform to the delegate protocol. It should put the name of the protocol in its class line and implement the methods from the protocol.
Tell object B that object A is now its delegate.
Here is a tutorial to give you a working example https://www.youtube.com/watch?v=9LHDsSWc680

Go to your storyboard, select the second view controller, go to the Identity inspector tab and give a StoryBoard ID value. This should be a unique value to identify your view controller.
Now in your first view controller', you can run this code. This will basically create an object of the second view controller, set the property value (for transferring data) and push it (same as the segue does)
let ctrl = self.storyboard?.instantiateViewControllerWithIdentifier("detailsView")
as? SecondViewController
ctrl?.userId = 250 // data to pass.
self.navigationController?.pushViewController(ctrl!, animated: true)
provided userId is a variable in your SecondViewController class. Replace
detailsView with the storyboard id value you gave earlier.
class SecondViewController: UIViewController {
var userId : Int = 0
override func viewDidLoad() {
super.viewDidLoad()
// do something with self.userId
}
}

Related

Problem with transfer of values ​in controllers in Swift

I have login few screens and controllers in my app. First screen is screen with button and moves user to next login view with username, password field and login button. On the controller i have function onClickButton and when i have good data i request to the server with this data.
When server give me callback i have many params about user to set in label in next view.
My structure is like this
Login View -> SecondLogin View and LoginViewController -> TabBarController -> NavigationController -> Table View with TableViewController
My code is
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "afterLoginView" {
if let secondVC = segue.destination as? TabBarViewController {
secondVC.finalName = self.username
}
}
}
When i want transfer my data directly to tableViewController i have error
Thread 1: signal SIGABRT
I do not understand what I'm doing wrong
You'll need these values in almost all view controllers. Create a singleton class to store the logged in user values like this
class UserDetails: NSObject, Codable {
static let shared = UserDetails()
private override init() {
super.init()
}
var finalName: String?
var otherDetails: String?
}
Now when you receive the response from the login api, assign the values in this singleton class.
UserDetails.shared.finalName = "something"//Name received from server callback
Now you can access these values from any view controller.
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(UserDetails.shared.finalName)
}
}
You have some work to do to get to the right view controller. Since your segue is only pointing at the UITabBarViewController, you should put in another guard or if/let statement to get you to the UINavigationController, and then another to finally get you to the UITableViewController, where you can actually refer to your finalName variable.
That would look something like:
if let secondVC = segue.destination as? TabBarViewController {
if let navCon = secondVC.viewController[0] as? UINavigationController {
if let tableVC = navCon.topViewController as? nameOfYourTableVC {
tableVC.finalName = self.username
}
The code is untested, just typed off the top of my head, so please proceed with due caution. Issues such as which tab is the correct NavController would also need to be addressed.
You need to use the actual name of your tableView class in that last if/let. A generic UITableViewController will not include your custom variables.
When server give me callback i have many params about user to set in label in next view.
This is a great example of why you should keep the M in MVC. When you get a response back from the server, store the returned data in your data model. (If you don't have a data model, you should make one.) When a view controller gets some data from the user, such as a user name, it should store that in the model. There's little reason to pass raw data back and forth between view controllers directly... just make sure that all your view controllers have a reference to the model, and have them get and set values there as needed.
This kind of approach will make your code a lot more flexible. It allows view controllers to worry about what they need to do their job, and it gets them out of the business of caring what other view controllers need.
My structure is like this
Login View -> SecondLogin View and LoginViewController -> TabBarController -> NavigationController -> Table View with TableViewController
It might make more sense to load the tab bar controller and then present the login view controller(s) modally. The view controllers that are managed by the tab bar controller can all be set up to refuse to do anything useful until the data they need is present in the data model, and that lets the tab bar controller be the root view controller. That will make it easy to set the model for each of it's child view controllers when the app starts up, and the app can then present the modal login view controllers, also set up with references to the model.

Xcode 10, Swift 4 - How do I transfer data across multiple view controllers? [duplicate]

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 3 years ago.
I am currently working on an app and have problems with posting events.
I collect data in a sequence of several views (7 screens) and would like to store the data finally at once (or show all the data in the final view).
These data are, for example, location info, user info, event image, comments, event category, ...
I know how to store the data in the database/storage (firebase) if I collect the data in one or two views.
But in my use case I have seven views and I could not find any elegant method.
What's the best way to do that with Xcode 10?
You can use struct as below code. Make all required variable for all screen in this struct (like string, image etc..). And you can access this from any ViewController.
struct InputDetails {
static var details: InputDetails = InputDetails()
var city: String = ""
var lat: String = ""
var long: String = ""
}
Now to add value in this
InputDetails.details.city = textfiels.text
Now to access first screen value in last screen
print(InputDetails.details.city)
And once your API call or above struct usage is over, make sure to reset all details like below.
InputDetails.details = InputDetails()
There are several ways for passing data between View Controllers. For example you could use an instance property or a segue or the delegation method.
I recommend you study this article which paints a complete picture of the different methods and how to apply them:
How To: Pass Data Between View Controllers In Swift
Edit:
Upon examining the picture in your question I figured that using a segue would be the most appropriate solution here. As it seems from the picture you enter data in one View Controller, pass that onto the second View Controller and finally you upload all the data to Firebase.
I assume that you use storyboards (if not then consult the link above for other methods.) In this example below you will pass a string from one VC to another.
Step 1:
Add a segue between two view controllers. Storyboard -> press ctrl and click on VC one and drag your mouse -> you will see a blue arrow, drag that to VC two and release -> select manual segue: show -> click on the segue and give it an identifier
Step 2:
In VC two, make a string variable:
class SecondViewController: UIViewController {
var stringToPass: String = ""
override func viewDidLoad() {
super.viewDidLoad()
print(stringToPass)
}
Step 3:
In VC one, enter the following:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? SecondViewController {
vc.stringToPass = "This is the string we pass between two VC"
}
}
Step 4:
Then whenever you want to go to the SecondViewController perform the segue like this:
performSegue(withIdentifier: "identifierYouEntered", sender: self)

How to cause an update of model to trigger a update in another controller in swift

View Controller A has access to a model and can mutate the model. View Controller B displays the model. I want when View Controller A updates the model, a method to be triggered in View Controller B such that the view is updated. Is there a standard way to do this in Swift?
In the didset of this variable you can post a notification and the other controller can register for this notification. (We can do it more neatly depending on the requirement.)
If you make your model object an Objective-C object (#objc) then you can use KVO (key-value observering) to observe changes to it. If you do that then any object that's observing a property of the model will get notified when that property changes.
Here is one way you could do this.
Have whatever model you use, class or struct, implement a protocol that can be be used by view-controller B. view-controller B should not care what the model is, only that it conforms to the protocol it needs to extract the data it needs.
When view-controller A manipulates the model, post a notification with a reference to the model as an instance of the protocol, in the userInfo dictionary. view-controller B, subscribes to the notification and when triggered, extracts the reference from the userInfo and then gets the data it needs via the common protocol.
Another way to communicate between 2 view controllers is Protocol. Define a protocol, such as:
protocol ViewControllerAModelProtocol: class {
func viewControllerAModelDidUpdate()
}
In ViewControllerA, define a delegate type:
weak var updatedProtocol: viewControllerAModelDidUpdate?
When the model is updated and call:
updatedProtocol?.viewControllerAModelDidUpdate()
In ViewControllerB, implement the protocol:
class ViewControllerB: UIViewController, ViewControllerAModelProtocol {
func viewControllerAModelDidUpdate() {
// reloadData()
}
}
If ViewControllerB is presented or showed by ViewControllerA, set A's updatedProtocol to B:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let b = segue.destination as? ViewControllerAModelProtocol {
self.updatedProtocol = b
}
}

Swift - passing values via segue

I'm trying to pass few variables via a segue. Initially I capture 6 variables on the first screen which I would like to pass on to the second view controller.
Each variable is captured through a text box capturing an integer and I called them T1, T2, T3 ... T6. At present I refer to the value through T1.text.toInt()!. Before I pass these values via segue, should I first create a variable like var T1 = T1.text.toInt()! ?
What is the best way of designing this?
inside prepareForSegue you have access to the UIController instance that will open:
class FirstPageUIViewController:UIViewController {
...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var controller = (segue.destinationViewController as! MyNextViewController)
controller.T1 = "value to pass"
}
}
This means you define you variables in MyNextViewController (the controller for your second screen) and the variables are already set when your MyNextViewController instance takes over control.
You also might decide to name your variables starting with small letters according to the swift style guide.

Using delegate between sibling view controllers in containers

I'm trying to make an app that uses three containers to show different content, but I'm having trouble communicating between the containers. I succeeded to use a segue to send some information at the tap of a button in one container to another container, but part of this information also has to be relayed to the third container. For this I wanted to use a delegate, but I cannot reference the right ViewController to the delegate variable.
So what I want goes as follows:
CollectionViewCell tapped, triggering segue to TableVC
TableVC receives information and updates the table
TableVC triggers delegate function in third VC
Third VC takes in some info and updates view
In the above I have managed to get 1 and 2 to work, but got stuck at 3.
I have made my protocol as follows:
protocol PurchaseDelegate {
func addToTotalAmount(product : Product)
}
In the TableVC I have declared var delegate : PurchaseDelegate? = nil and in the IBAction triggered from the segue: delegate?.addToTotalAmount(product)
In the third VC I have implemented the delegate as follows:
class thirdVC:UIViewController,PurchaseDelegate {
func addToTotalAmount(product : Product) {
println("Adding....")
}
}
All three containers are within a main VC that does some initial stuff in the application.
My problem is, that I don't know how to get a reference from thirdVC to my delegate variable in my tableVC.
Thanks in advance.
I ended up finding the solution to the problem after a bit further searching with inspiration from #Anna Dickinson.
Firstly, the containers must be ordered correctly in the storyboard. The container whose view controller implements the delegate protocol must be first in the list and then the other view controller further down.
Then, in the main view controller - the view controller for the view with the containers - the prepareForSegue function is implemented, since it will be triggered as the containers are initialized.
This all of the code remains as above, but the main view controller will be something like the following:
class MainViewController: UIViewController {
var actionVC : FirstViewController! // This is the one, that implements the delegate protocol
var tableVC : SecondViewController! // This is the one, that has a delegate variable
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "firstVC"){
self.actionVC = segue.destinationViewController as FirstViewController
} else if(segue.identifier == "secondVC"){
self.tableVC = segue.destinationViewController as SecondViewController
self.tableVC.delegate = self.actionVC
}
}
}
I'm not sure if the is the right, nor the best way to do this, but it works perfectly for what I need.

Resources