0

我的 App 中有两个 viewController,第一个 viewController 的代码如下图所示:

           import UIKit

            class firstViewController: UIViewController {

              // The below two variables will be passed from the firstViewController to the secondViewController then back again from the secondViewController to the firstViewController:

              var selectedRowValue: Int = 0

              var selectedSectionValue: Int = 0

              let main = UIStoryboard(name: "Main", bundle: nil)

               lazy var secondViewController = main.instantiateViewController(withIdentifier: "secondViewController")

             override func viewDidLoad() {

             super.viewDidLoad()

              }

    // The below function will be triggered when the user tap on a specific tableView cell detailClosure icon. This is when the needed data get sent from this viewController to the secondViewController:

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

             let secondViewControllerProperties = secondViewController as! secondViewController

              secondViewControllerProperties.receivedSelectedSectionValueFromFirstVc = indexPath.section

              secondViewControllerProperties.receivedSelectedRowValueFromFirstVc = indexPath.row

    // The below is the relevant content of a UILabel inside the tapped tableView cell by the user that get send to the secondViewController for it to be displayed as its NavigationBar title:

             secondViewControllerProperties.selectedUniversalBeamSectionDesignation = arrayWithAllDataRelatedToUbsSections.filter({ $0.sectionSerialNumber == "\(arrayWithAllSectionsSerialNumbers[indexPath.section])" }).map({ $0.fullSectionDesignation })[indexPath.row]

self.present(secondViewControllerProperties, animated: true, completion: nil)

             }

            }

    // The below extension inside the firstViewController is used to pass data back from the secondViewController to the firstViewController:

        extension firstViewController: ProtocolToPassDataBackwardsFromSecondVcToFirstVc {

            func dataToBePassedUsingProtocol(passedSelectedTableSectionNumberFromPreviousVc: Int, passedSelectedTableRowNumberFromPreviousVc: Int) {

                self.selectedRowValue = passedSelectedTableRowNumberFromPreviousVc

                self. selectedSectionValue = passedSelectedTableSectionNumberFromPreviousVc

        }

        }

下面是第二个视图控制器中的代码:

import UIKit

class secondViewController: UIViewController {

    weak var delegate: ProtocolToPassDataBackwardsFromSecondVcToFirstVc?

// The below variables get their values when the data get passed from the firstViewController to the secondViewController:

    var receivedSelectedRowValueFromFirstVc: Int = 0

    var receivedSelectedSectionValueFromFirstVc: Int = 0

   var selectedUniversalBeamSectionDesignation: String = ""

// Inside the below navigationBar declaration, its labelTitleText will depend on the tapped tableViewCell by the user inside the firstViewController:

lazy var navigationBar = CustomUINavigationBar(navBarLeftButtonTarget: self, navBarLeftButtonSelector: #selector(navigationBarLeftButtonPressed(sender:)), labelTitleText: "UB \(selectedUniversalBeamSectionDesignation)", navBarDelegate: self)

    override func viewDidLoad() {

        super.viewDidLoad()

        view.addSubview(navigationBar)

}

// The below gets triggered when the user hit the back button inside the navigationBar of the secondViewController. This is where using the Protocol data get passed back to the firstViewController: 

extension secondViewController: UINavigationBarDelegate {

    @objc func navigationBarLeftButtonPressed(sender : UIButton) {

        if delegate != nil {

            delegate?.dataToBePassedUsingProtocol(passedSelectedTableSectionNumberFromPreviousVc: self.selectedTableSectionNumberFromPreviousViewController, passedSelectedTableRowNumberFromPreviousVc: self.selectedTableRowNumberFromPreviousViewController)

        }

        dismiss(animated: true) {}

    }

}

但是,我注意到的是,当用户点击 secondViewController 的导航栏中的后退按钮时,每当 secondViewController 被解除。secondViewController 没有被取消初始化,因此,每当我在 firstViewController 内的 tableView 内按下不同的单元格时,在 secondViewController 内显示的导航栏标题仍然与我第一次按下时显示的标题相同。由于 secondViewController 没有取消初始化,因此,我看到的值与第一次初始化时相同。

我的问题是如何在 secondViewController 被解除时取消初始化它,以便每次我点击 firstViewController 内的 tableView 内的不同单元格时,都会初始化一个新的 secondViewController?

4

1 回答 1

0

您的代码生成secondViewController一次并重用它(它是一个属性)。

               lazy var secondViewController = main.instantiateViewController(withIdentifier: "secondViewController")

这意味着它将一直存在,直到第一个视图控制器被销毁,当然 - 将被重用。
相反,您应该根据需要创建它。

func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
             // Create the second view controller
             let secondViewController = main.instantiateViewController(withIdentifier: "secondViewController")

             let secondViewControllerProperties = secondViewController as! secondViewController

              secondViewControllerProperties.receivedSelectedSectionValueFromFirstVc = indexPath.section

              secondViewControllerProperties.receivedSelectedRowValueFromFirstVc = indexPath.row

    // The below is the relevant content of a UILabel inside the tapped tableView cell by the user that get send to the secondViewController for it to be displayed as its NavigationBar title:

             secondViewControllerProperties.selectedUniversalBeamSectionDesignation = arrayWithAllDataRelatedToUbsSections.filter({ $0.sectionSerialNumber == "\(arrayWithAllSectionsSerialNumbers[indexPath.section])" }).map({ $0.fullSectionDesignation })[indexPath.row]

self.present(secondViewControllerProperties, animated: true, completion: nil)

             }

            }

删除lazy var当然,它不再需要。
此外,您可以这样做:
let secondViewController = main.instantiateViewController(withIdentifier: "secondViewController") as! SecondViewController与其稍后再投射,不如让它更干净一些。

于 2020-03-25T20:44:58.980 回答