transfer weather data to container view - ios

Im unable to update labels in container view. Here's how i've done it.
​
I wrote my updateWeather function in main VC and retrieved the weather data successfully. when i printed weatherJSON it shows all the received data in console.
now when i started writing updateUI function i could only update the labels on main VC.
so i used prepare segue to send data to container view and sent a string to container VC and updated "humidity" label successfully. all labels accept strings without any issues.
but i have no idea how to send weather data to container view.
i tried passing values using object weatherDataModel but nothing happens. i even declared a new object referring to container view class and used it in updateUI function to set label values but it won't work too.
I have no idea what to pass in place of string to get weather data through to next VC.
override func prepare(for segue: UIStoryboardSegue, sender for: Any?) {
if segue.identifier == "displayFullWeatherInfo"{
let destinationVC = segue.destination as! FullWeatherViewController
destinationVC.delegate = "\(weatherDataModel.pressure)"
....
....
Heres my WeatherDataModel Class:
import Foundation
class WeatherDataModel{
var city = ""
var temp = 0
var country = ""
var humidity = 0
}
in my main VC i have created weatherDataModel object and here's my updateWeatherInfo code:
func updateWeatherInfo(json : JSON){
if let tempDefault = json["data"][0]["temp"].double{
weatherDataModel.temp = Int(tempDefault)
weatherDataModel.city = json["data"][0["city_name"].stringValue
weatherDataModel.country = json["data"][0]["country_code"].stringValue
weatherDataModel.humidity = json["data"][0]["rh"].intValue
updateWeatherUI()
}
else{
currentLocation.text = "Not Available"
}
}
​

Create a property for your weather model object in your second VC FullWeatherViewController:
var weatherDataModel: WeatherDataModel! //your object
And in your first VC:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "displayFullWeatherInfo" {
let destinationVC = segue.destination as! FullWeatherViewController
destinationVC.weatherDataModel = weatherDataModel
}

Related

Swift. Passing persistent data from one View Controller to another and storing within a label array

I am using a segue to go from View Controller 1 to View Controller 2. View Controller 1 has a button that sets the persistent data when it is clicked on:
I declare a global var for user default:
let userDefault = UserDefaults()
Here is my button to set the user default to a string with text values from labels:
#IBAction func saving(_ sender: UIButton) {
let savedText = "Gallon \(gallonTextFieldOutlet.text) is equal to Litre \(litreTextFieldOutlet.text) is equal to Pint \(pintTextFieldOutlet.text)"
userDefault.setValue(savedText, forKey: "SavedConversion")
}
I then get a reference to View Controller 2 and pass this user default when the user goes from View Controller 1 to View Controller 2 via a segue:
// in view controller 2: reference to get persistent data
var volumeDataOne:String?
// in view controller 2: instantiation of my queue class to use methods
var queue = Queue<String>()
// segue action in view controller 1
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "savedVolumeData"
{
let historyVC:VolumeHistoryViewController = segue.destination as! VolumeHistoryViewController
if let value = userDefault.value(forKey: "SavedConversion") as? String {
historyVC.volumeDataOne = value
}
}
I get this in the View Controller 2 and I am trying to set this to three labels that I have in this View Controller:
func DisplayVolumeHistory() {
let labelArray = [volumeDataLabelOutlet, volumeDataLabelTwoOutlet, volumeDataLabelThreeOutlet]
if let bindingOptional = volumeDataOne
{
for index in 0..<labelArray.count
{
queue.enqueue(val: bindingOptional)
labelArray[index]?.text = queue.arr[index]
}
}
}
In my specification, I have been told that the data needs to be persistent and that only the last five data can be stored at one time. So I have a class that I have called Queue which is referenced in this function. This function gets called on the viewDidLoad of the View Controller 2.
override func viewDidLoad() {
super.viewDidLoad()
//Debug ...
volumeDataLabelOutlet.text = "na"
volumeDataLabelTwoOutlet.text = "na 2"
volumeDataLabelThreeOutlet.text = "na 3"
//...
DisplayVolumeHistory()
// Do any additional setup after loading the view.
}
I have tested my Queue class in a Playground and it works as expected. The Queue class can be seen here:
class Queue {
var arr = [T]()
func enqueue(val: T){
if(arr.count < 3) {
arr.append(val)
} else {
for i in 0..<arr.count-1 {
arr[i] = arr[i + 1]
}
arr[arr.count - 1] = val
}
}
func dequeue() -> (T?){
if (arr.isEmpty){
return nil
} else {
return arr.remove(at: 0)
}
}
}
Here is my issue that I cannot seem to figure out. In View Controller 2, all of the three labels will have persistent data, but they will all be of the same data,
For example, if I have data as follows:
DATA 1: 555
DATA 2: 700
DATA 3: 62
I would want:
LABEL 1 --> 555
LABEL 2 --> 700
LABEL 3 --> 62
However, currently it will be:
LABEL 1 --> 62
LABEL 2 --> 62
LABEL 3 --> 62
I am unsure as to why debugging. I believe it is because my persistent data in my View Controller 1 is only taking a string, which the Dictionary is overriding as I use the same key.
However, I looked at the documentation and trying to use a user default array did not solve my issue and I am unsure as to what is causing this problem.
I appreciate any guidance and help to try to solve this issue.
Thanks
You are right on your comment,
which the Dictionary is overriding as I use the same key
Each time you tap that button, you are overriding the value with the new one. So you should be seeing always on your v2, the last one.
So probably you should store an array instead of a String.
//on v1
var values = [String]()
#IBAction func saving(_ sender: UIButton) {
let savedText = "Gallon \(gallonTextFieldOutlet.text) is equal to Litre \(litreTextFieldOutlet.text) is equal to Pint \(pintTextFieldOutlet.text)"
values.append(savedText)
UserDefaults.standard.setValue(values, forKey: "SavedConversion")
}
You will pass it to v2 as you are doing now
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "savedVolumeData"
{
let historyVC:VolumeHistoryViewController = segue.destination as! VolumeHistoryViewController
if let values = userDefault.value(forKey: "SavedConversion") as? [String] {
historyVC.volumeDataOne = values //volumeDataOne now needs to be an [String]
}
}
Then on V2, go through your array and labels.

Prepare for segue function not loading new values

The statValue attribute of my UIButton class has been updated since the segue was last called, but the segue still sends the old, original value. Is there a way to refresh the prepare function (below) so that it sends the new value?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "StatDetailSegue" {
if let destinationVC = segue.destination as? StatDetailViewController,
let index = (sender as? StatButton)?.buttonIndex,
let sendVal = (sender as? StatButton)?.buttonValue,
let sendUnit = (sender as? StatButton)?.buttonUnit,
let sendTitle = (sender as? StatButton)?.detailTitle {
destinationVC.statID = index
destinationVC.statValue = sendVal
destinationVC.statUnit = sendUnit
destinationVC.statTitle = sendTitle
print("Destination STATID: \(destinationVC.statID)")
print("Destination value: \(destinationVC.statValue)")
}
}
}
Check your StatButton if you using in storyboard your buttons , then your button should inherit from StatButton instead of UIButton otherwise your code looks fine.
Can you debug the value of statValue in your destinationVC and check if it gets updated after the destinationVC is presented? Also, check the implementation of 'StatButton' class, maybe the buttonValue property is a lazily initialized property and initialized only once? That's why maybe you keep getting the first value that was assigned to the buttonValue always.

Sending data to another view controller

This is a learning app that I am making for fun, I have been stuck here for 2 days.
I have two views setup that I use to send data that the user will pick to the other one (they are named AddCoinVC and MainVC).
In AddCoinVC, the sending is performed when the user clicks on the button
let vc = MainViewController()
vc.coinArray.append(CoinWallet(coinName: "Test", coinSymbol: "Test", coinAmount: "0"))
performSegue(withIdentifier: "backToMain", sender: self)
I have setup a breakpoint at this point and printing vc.coinArray prints me the correct value =
($R0 = 1 value { (coinName = "Test", coinSymbol = "Test", coinAmount = "0")
}
But when I go to my other breakpoint at MainVC, it displays 0 value.
var coinArray = [CoinWallet]()
This is the var that I use, the default in MainVC is CoinWallet which is empty when first loading the app. This is the custom Class.
class CoinWallet {
var coinName:String = ""
var coinSymbol:String = ""
var coinAmount:String = ""
init(coinName: String, coinSymbol:String, coinAmount: String) {
self.coinName = coinName
self.coinSymbol = coinSymbol
self.coinAmount = coinAmount
}
}
When the segue and the sending is performed from AddCoinVC to MainVC 'coinArray' should have this value sent to it.
Why would be the value empty if vc.coinArray has 1 value?
The error occurs because MainViewController() does not return the view controller you expect. It's a new blank instance which is not the instance in the storyboard.
Since you are performing a segue anyway, pass the CoinWallet instance as sender parameter in performSegue
let coin = CoinWallet(coinName: "Test", coinSymbol: "Test", coinAmount: "0")
performSegue(withIdentifier: "backToMain", sender: coin)
Then implement prepare(for segue and use the destination property as reference to the main view controller.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "backToMain" {
let coin = sender as! CoinWallet
let mainViewController = segue.destination as! MainViewController
mainViewController.coinArray.append(coin)
}
}
You should add this method in your AddCoinVC and send data as below,
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let mainVC = segue.destination as? MainViewController {
mainVC.coinArray.append(CoinWallet(coinName: "Test", coinSymbol: "Test", coinAmount: "0"))
}
}
In the below lines of code, you are just creating a new instance that is not the viewController being segued.
let vc = MainViewController()
vc.coinArray.append(CoinWallet(coinName: "Test", coinSymbol: "Test", coinAmount: "0"))
You should only perform the segue on button click as below and set any data inside the above method.
performSegue(withIdentifier: "backToMain", sender: self)

Assign strings to textfields in another view controller

I am trying to take facebook values, such as name and email, and transferring them to another view controller where they populate their corresponding text fields. However, when I try to populate the text fields with this code
let fbEmail = data["email"]
let fbName = data["name"]
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as? CreateAccountVC
vc!.emailTxt = self.fbName
vc!.fullnameTxt = self.fbEmail
}
I receive an error saying "Cannot assign type String to type UITextField!" I'm confused here as I thought that text fields only take strings (I'm very new to coding/programming).
So basically, how do I get these values gathered in one view controller to populate text fields in another view controller? Any help would be greatly appreciated! Thanks!
Error reason is you need to assign it to the UITextField's text property.
You can also use variables to set them. And set textfield's values in your CreateAccountVC controller's viewDidLoad function. Like this:
Your first controller where you take facebook values:
let fbEmail = data["email"]
let fbName = data["name"]
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? CreateAccountVC {
vc.emailTxtValue = self.fbName
vc.fullnameTxtValue = self.fbEmail
}
}
Your CreateAccountVC :
class CreateAccountVC: UIViewController {
var emailTxtValue:String?
var fullnameTxtValue:String?
override func viewDidLoad() {
super.viewDidLoad()
emailTxt.text = emailTxtValue
fullnameTxt.text = fullnameTxtValue
}
}

Passing data between view controllers through segue

I have a MapViewController with a prepareForSegue(_:sender:)method, which I intend to use to send data to LandmarkTableViewController, and is called when a button is pressed.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationvc = segue.destinationViewController
if let landmarkvc = destinationvc as? LandmarkTableViewController {
if let identifier = segue.identifier {
let library = Landmark(name: "Run Run Shaw Library", properties: ["Chinese Kanji", "Gray", "Green Plants"])
let bank = Landmark(name: "Hang Seng Bank", properties: ["Chinese Kanji", "Green"])
switch identifier {
case "showLibrary" : landmarkvc.passedLandmark = library // pass data to LandmarkTableViewController
case "showBank" : landmarkvc.passedLandmark = bank // pass data to LandmarkTableViewController
default : break
}
}
}
}
The LandmarkTableViewController is properly set up to display the String array properties, with one String on each row. So what I intend to do is pass the appropriate data for the table to properties according to which button was pressed, and let LandmarkTableViewController display the corresponding properties.
class LandmarkTableViewController: UITableViewController {
var properties = [String]()
var passedLandmark = Landmark(name: "temp", properties: ["temp"]) // initially set to default value
override func viewDidLoad() {
super.viewDidLoad()
loadSampleProperties()
}
func loadSampleProperties() {
self.properties = passedLandmark!.properties
}
// other methods....
}
class Landmark {
var name: String
var properties: [String]
init?(name: String, properties: [String]) {
self.name = name
self.properties = properties
// Initialization should fail if there is no name or if there is no property.
if name.isEmpty || properties.isEmpty {
return nil
}
}
However, when I run the code, only temp is displayed in the table view. I've been stuck on this for a long time now, so any help is much appreciated!
Edit: loadData() inside of viewDidLoad() is changed to the correct loadSampleProperties(). I made an error while posting the code to the question.
I think this should solve your problem if not double check your identifiers
and you can make sure to data passing with adding print(passedLandmark) to viewDidLoad() or breakpoint to make sure you getting the data
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationvc = segue.destinationViewController
if let landmarkvc = destinationvc as? LandmarkTableViewController {
if segue.identifier == "showLibrary" {
let library = Landmark(name: "Run Run Shaw Library", properties: ["Chinese Kanji", "Gray", "Green Plants"])
landmarkvc.passedLandmark = library
}
if segue.identifier == "showBank" {
let bank = Landmark(name: "Hang Seng Bank", properties: ["Chinese Kanji", "Green"])
landmarkvc.passedLandmark = bank
}
}
}
Hope this will helps
Code is missing from your quote, so I can't be sure, but I assume your loadData() method is the one that reloads the table view data with Landmark you've passed in prepareForSegue. If that is the case:
viewDidLoad() is called before prepareForSegue, so that all the views and elements of the destinationViewController are loaded and ready to use. Thus, in your case, the table view is loaded with your "temp" data and nothing makes it reload when you set the proper one.
You have two options:
You could call loadData()/reloadData() in viewWillAppear for example, which is called after prepareForSegue(). Bare in mind that viewWillAppear will possibly be called again in some other navigation.
Otherwise, you could instantiate and present/push the new controller in your parent view controller, instead of using the segue.

Resources