swift4 Understanding the passing of values between different views

Asked 2 years ago, Updated 2 years ago, 56 views

As I wrote in swift4, it is not possible to pass values between different views.
I would like to save the text I wrote in ViewController1 to userdefaults and display it in ViewController2's tableView.
I would like to pass the contents of the variable called fruits and use it as the key for userdefaults at the destination, but I cannot read userdefaults and display tableView because of the failure to pass through them.The fruits change by pressing the UI button on the View.

//ViewController1
classViewController1:UIViewController {
  var fruits: String="apple"
  var Array: String = [ ]
...omitted...
      @ IBAction func apple(_sender:Any){
        fruits="apple"
      }
      @ IBAction funcbanana(_sender:Any){
        fruits="banana"
      }
...omitted...
  let data = UserDefaults.standard 
  iflet Array=data.stringArray(forKey:fruits){
    self.Array=Array
  }
  // Press Save button and append to Array
  Array.append(myText)
  data.set (Array, forKey:fruits) 
  data.synchronize()
...omitted...
      override func prepare (for segment:UIStoryboardSegue, sender: 
      Any?) {
        if(segue.identifier=="toVC2"){
          letViewController2=(segue.destination as? 
          ViewController 2)!
          ViewController2.fruits=self.fruits
        }
      }
}

If the contents of the variable fruits are banana at the time of transition, I would like to display only the userdefault key:banana in the cells displayed in ViewController 2.

//ViewController 2
classViewController2 —UIViewController, UITableViewDelegate, 
UITableViewDataSource{
  @IBOutlet weak variableView:UITableView!
  var Array: String = [ ]
  var fruits: String=""
  override func viewWillAppear(_animated:Bool){
    super.viewWillAppear(animated)
    readData()
  }
  ...omitted...
  funcreadData(){ 
        let data = UserDefaults.standard
        iflet Array=data.stringArray(forKey:fruits){
            self.Array=Array
    }
    Update tableView.reloadData()//cell
  }
  ...omitted...
  functableView(_tableView:UITableView, numberOfRowsInSection 
  section: Int) - > Int {
    return Array.count
  }

  functableView(_tableView:UITableView, cellForRowAtindexPath: 
  IndexPath) - > UITableViewCell {
    
    letcell: UITableViewCell= 
    tableView.dequeueReusableCell(withIdentifier: "Cell", for: 
    indexPath)
    cell.textLabel!.text=Array [indexPath.row]
    return cell
  }
}

Thank you for looking at my question.
There are many parts that I don't understand, so it might be a strange code.
I look forward to hearing from you.

swift xcode swift4

2022-09-29 21:32

1 Answers

DefaultsController is a class that returns a value corresponding to the key, so

data.set (Array, forKey:fruits)

and

iflet Array=data.stringArray(forKey:fruits)

If fruits in is not the same value (=key), what I expected will not come back.
In other words, the state in UserDefaults.standard at a given moment is

 --UserDefaults.standard -----------------------
|   key | value |
|-----------+-----------------------------|
| banana|@["banana"]|
|-----------+-----------------------------|
| apple|@["banana", "apple"]| 
-------------------------------------------
(orange is undefined, so nil is returned.)

As shown in , if you change the key (in this case, the variable fruits) every time you add elements to an array, the data will be created for each key and you won't know which one to take out.

Therefore, the correct answer in this case is between the import statement and the class declaration to either the ViewController1.swift or the ViewController2.swift source.

publiclet fruits:String="fruits"

You should write fruits as a constant, and ViewController1, ViewController2 should not substitute fruits.

Here's an aside

However, as the name suggests, UsersDefault is a mechanism to remember application settings even after the application exits, and I think you should use other mechanisms to manage user data, such as proprietary files, CoreData, SQLite3 databases.


2022-09-29 21:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.