执行 DidSelectRowAt 时,变量不再有它们的值

When executing DidSelectRowAt, variables no longer have their values

我目前正在做一个项目,但是我遇到了这个问题。执行 didSelectRowAt 时,未在初始化中分配的变量将被删除。 基本上,如果我这样做了,我需要了解我之前 select 所做的那一行的价值。我做了这个项目来简化问题。我有两个 ViewController,当我在第一个 ViewController 处单击 tableView 中的任何行时,它会切换到第二个 ViewController。原始项目中的值正在发生变化。回到第一个 ViewController 时,我需要将此 indexValue 与我单击的行进行比较。任何建议将被认真考虑。 :)

第一个ViewController

var globalIndex: Int!
var delegate: passDataDelegate!
let valueForExample = "This is just to show, how program behaves."

func passData(index: Int?) {
    globalIndex = index
    //When printing globalIndex, it shows normal value
}

let array = ["Row 1", "Row 2", "Row 3", "Row 4"]

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return array.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "customTableView", for: indexPath) as! CustomTableViewCell
    cell.label.text = array[indexPath.row]
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    print("Global index is \(globalIndex)")
    //When printing globalIndex, the value is always nil, however when I print valueForExample it normally shows its value
    if(globalIndex == indexPath.row) {
        print("You pressed the same button as before")
    }
    let secondVC = storyboard?.instantiateViewController(withIdentifier: "secondVC") as! SecondViewController
    delegate = secondVC
    delegate.passData(index: indexPath.row)
    secondVC.modalPresentationStyle = .fullScreen
    present(secondVC, animated: true)
}

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    initialConfiguration()

}

func initialConfiguration() {
    tableView.delegate = self
    tableView.dataSource = self
    tableView.register(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "customTableView")
}

第二个ViewController

protocol delegate {
    func passData(index: Int?)
}
class SecondViewController: UIViewController, passDataDelegate {

    var globalIndex: Int!
    var delegat: delegate!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    @IBAction func button(_ sender: UIButton) {
        let firstVC = storyboard?.instantiateViewController(withIdentifier: "firstVC") as! ViewController
        delegat = firstVC
        delegat.passData(index: globalIndex)
        dismiss(animated: true, completion: nil)
    }

    func passData(index: Int) {
        globalIndex = index
    }
}

第一ViewController

var globalIndex: Int? //Make global index optional

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    print("Global index is \(globalIndex)")

    if(globalIndex == indexPath.row) {
        print("You pressed the same button as before")
    }

    let secondVC = storyboard?.instantiateViewController(withIdentifier: "secondVC") as! SecondViewController
    secondVC.delegat = self // Set delegate
    secondVC.globalIndex = indexPath.row //Set global index
    secondVC.modalPresentationStyle = .fullScreen
    present(secondVC, animated: true)
}

extension FirstViewController: passDelegate {
    func passData(index: Int?){
        globalIndex = index // Set index from second view controller to first viewcontroller
    }
}

秒ViewController

protocol passDelegate {
    func passData(index: Int?)
}

class SecondViewController: UIViewController {
   var globalIndex: Int!
   var delegat: delegate!

   override func viewDidLoad() {
      super.viewDidLoad()
   }

   @IBAction func button(_ sender: UIButton) {
       delegat.passData(index: globalIndex) //Call delegate
       dismiss(animated: true, completion: nil)
   }
 }