Swift Transfer the data to the first view by pressing & ldquo; Back & rdquo; button

advertisements

Hello friends . Navigation control to the second screen. I need to go to the one screen with the button(Sonucları Listele) on the second screen and I have to make the number on the first screen to "2"

image 1 image 2

First View

class KonularViewController: UIViewController {

    var number : Int?

    @IBAction func barButtonKonuEkle(_ sender: Any) {
        let childViewController = storyboard?.instantiateViewController(withIdentifier: "KonuEkleViewController") as! KonuEkleViewController
        navigationController?.pushViewController(childViewController, animated: true)
    }
}

Second View

class AramaViewController: UIViewController {
    @IBOutlet weak var btn1: UIButton!
    @IBOutlet weak var btn2: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        btn1.isSelected = true
    }

    @IBAction func btnListele(_ sender: Any) {
      //First View "Number" variable on the first screen will be 2
    }

    @IBAction func btn_box(sender: UIButton) {

        if sender.titleLabel?.text == "En Yeniler"
        {
        btn1.isSelected = true
        btn2.isSelected = false
        }
        else
        {
        btn2.isSelected = true
        btn1.isSelected = false
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}


One strategy would be to have a 3rd class that is your Model, which can hold state.

class Model {
   static let shared = Model()
   var count: Int = 1
}

class AramaViewController: UIViewController {
    @IBAction fund btnListele(_ sender: Any) {
      Model.shared.count += 1
      //First View "Number" variable on the first screen will be 2
}

class KonularViewController: UIViewController {
    override func viewWillAppear(animated:Bool) {
        super.viewWillAppear(animated: animated)
        numberView.text = "\(Model.shared.count)"  //display your number here
    }
}

Another Option

Reach back in the Navigation Controller View Controllers array to the previous view and set a property. This is a bit more fragile.

class KonularViewController: UIViewController {
    var count: Int = 1
    override func viewWillAppear(animated:Bool) {
        super.viewWillAppear(animated: animated)
        numberView.text = "\(count)"  //display your number here
    }
}

class AramaViewController: UIViewController {
    @IBAction fund btnListele(_ sender: Any) {
      let numberOfViews = navigationController.viewControllers.count
      if count > 1, let previousViewController = self.navigationController.viewControllers[numberOfViews-2] as? KonularViewController
      previousViewController.count += 1
      //First View "Number" variable on the first screen will be 2
}