Swift按“返回”按钮将数据传输到第一个视图

时间:2017-03-21 20:03:34

标签: swift uinavigationcontroller

  

朋友你好。导航控制到第二个屏幕。我要走了   在第二个屏幕上按钮(SonuclarıListele)到一个屏幕,我必须   将第一个屏幕上的数字设为“2”

image 1 image 2

第一视图

class KonularViewController: UIViewController {

    var number : Int?

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

第二视图

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.
    }


}

2 个答案:

答案 0 :(得分:1)

一种策略是拥有一个属于你的模型的第三类,它可以保持状态。

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
    }
}

另一个选项

将导航控制器视图控制器阵列返回到上一个视图并设置属性。这有点脆弱。

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
}

答案 1 :(得分:0)

假设KonularViewController标识符为“KonularViewController”。 在AramaViewController中:

@IBAction func btnListele(_ sender: Any) {
      //First View "Number" variable on the first screen will be 2
      let konularViewController = storyboard?.instantiateViewController(withIdentifier: "KonularViewController") as! KonularViewController
      konularViewController.number = 2
      navigationController?.pushViewController(childViewController, animated: true)
}