0

I have two ViewControllers, each with one TextField to get user input number and two buttons to calculate and navigate.

The instance of TextField in the SecondVC is created by user when filling the TextFields and it exists and it is shown in the View

The problem is: when you leave the SecondView Controller after creating the object - Cell and you come back to it later, it is set back to cero, which is not the instance - Cell value

class MainViewController: UIViewController {

    @IBOutlet weak var A3TextField: UITextField!

    @IBAction func calc(_ sender: Any) {
        let A3 = Cell(name: "A3", sheet: "", value: Double(A3TextField.text!)!)
        print(A3)

    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }
}

class SecondViewController: UIViewController {

    @IBOutlet weak var B3TextField: UITextField!

    @IBAction func calc2(_ sender: Any) {
        let B3 = Cell(name: "B3", sheet: "", value: Double(B3TextField.text!)!)
        print(B3)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

    }

}

struct Cell {

    var name: String = ""
    var sheet: String = ""
    var value: Double = 0

    init(name: String, sheet: String, value: Double) {
        self.name = name
        self.sheet = sheet
        self.value = value
    }

}

enter image description here

Fausto Checa
  • 163
  • 13
  • If you pop a view controller from the navigation stack, it loses all of its data - this is for saving memory. You should save that data somewhere. – Tamás Sengel Oct 18 '18 at 16:39
  • Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Tamás Sengel Oct 18 '18 at 16:39
  • If you are not persisting data, how can you expect that data will be populated after coming back to viewcontroller. You should first save data locally and then in viewDidLoad of each controlller fetch this locally saved data first and then set it to respective textfield. – nikBhosale Oct 19 '18 at 05:01

1 Answers1

0

When controller pop or push ,their temporary property value or object can not be save to other controller,even in itself controller ,when push and back ,their are the init value,if you want save value when page switching,suggest these solution:

1. Pass throw the attribute values(one => two)

OneViewController:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
 let twoVC = storyboard.instantiateViewController(withIdentifier: "TwoViewControllerID") as! twoViewController
 twoVC.myStr="Controller one to controller two!" 
 self.present(twoVC, animated: true, completion: nil)

TwoViewController

 var myStr=String()
  override func viewDidLoad() {
     super.viewDidLoad()
     print("get value:\(myStr)"

}

2, use NSUerDfault to save and get

SetValue:

let userDefault = UserDefaults.standard
userDefault.set(name, forKey: "name")
userDefault.synchronize()

GetValue:

let userDefault = UserDefaults.standard
let name = userDefault.object(forKey: "name") as? String

3,storybord pass value(one => two)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
let theSegue=segue.destination as! twoViewController theSegue.myStr="other pass way" }

4,deleagte pass value(two => one) based don back to front page

TwoViewController:

//(1)create a delegate before class 
protocol FTVCdelegte : NSObjectProtocol{ 
//define method
// one method
 func change(title:String) 
 //two method
 func ChangeColoer (Coloer:UIColor)
 //three method
 func ChangSucces(YON:Bool) 
 } 

 //(2)create delegate object。 
var delegate_zsj:FTVCdelegte? 

//(3)click back button
 @IBAction func backBtnAction(_ sender: UIButton) { 
 delegate_zsj?.change(title: "main page") 
 delegate_zsj?.ChangeColoer(Coloer: UIColor.red) 
 delegate_zsj?.ChangSucces(YON: true) 
 self.navigationController?.popViewController(animated: true)
 }

OneViewController

//(4)inherit delegate: 
class SecondViewController: UIViewController,FTVCdelegte,ChangeBtnDelege{
...
//(5)imple delegate method 

 func change(title: String) { self.title = title } 

 func ChangeColoer(Coloer: UIColor) { self.view.backgroundColor = Coloer } 

 func ChangSucces(YON: Bool) { print(YON) } 

 //(6)when pop page use
 @IBAction func tiaozhuanBtnAction(_ sender: Any) { 
 let storyboard = UIStoryboard(name: "Main", bundle: nil)
 let oneVC = storyboard.instantiateViewController(withIdentifier:   "OneViewControllerID") as! oneViewController 
 oneVC.delegate_zsj = self 
 self.navigationController?.pushViewController(oneVC, animated: true) 
 }

5,block pass value(two => one) based don back to front page

TwoViewController:

//(1)define a block method:
var bbchange:((_ title:String,_ myColor:UIColor)->Void)? 

//(2)back to front page:
@IBAction func backBtnAction(_ sender: UIButton) { 
bbchange?("document",UIColor.green) 
self.navigationController?.popViewController(animated: true)

}

OneViewController

//(3)next page invoke block  method
@IBAction func tiaozhuanBtnAction(_ sender: Any) { 
let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let threeVC = storyboard.instantiateViewController(withIdentifier:    "twoViewControllerID") as! twoViewController 
twoVC.bbchange=
{ 
  ( title:String,myColor:UIColor) in 
  self.title=title 
  self.view.backgroundColor=myColor
} 
self.navigationController?.pushViewController(twoVC, animated: true)
}
Junior Jiang
  • 12,430
  • 1
  • 10
  • 30