通过导航后退按钮传递数据

时间:2016-01-22 21:08:40

标签: ios swift uinavigationcontroller pass-data

我处于这种情况:

img1

我正在通过准备segue将4个数组从进度表传递给细节练习,它工作正常!当我尝试将数据从详细运动控制器传递回进度表控制器时,问题就开始了。我想使用默认导航后退按钮返回父视图。实际上我使用的是这段代码,但它不起作用,数据从子视图传递到父视图,但我无法在进度表中看到结果,似乎我需要刷新但我也在viewDidLoad之后尝试了reloadData并且它没有工作。有什么建议吗?谢谢。

override func viewWillDisappear(animated : Bool) {
    super.viewWillDisappear(animated)

    if (self.isMovingFromParentViewController()){
        print("n'drio")

        let historyView = self.storyboard!.instantiateViewControllerWithIdentifier("historyView") as! HistoryTableViewController
        historyView.isFirstTime = false
        historyView.arrayData = arrayDataDetails
        historyView.arrayRipetizioni = arrayRipetizioniDetails
        historyView.arrayPeso = arrayPesoDetails
        historyView.arrayRecupero = arrayRecuperoDetails
        historyView.tableView.reloadData()
    }
}

4 个答案:

答案 0 :(得分:39)

当您按下后退按钮时,导航控制器将调用navigationController(willShowViewController:),以便您可以使用此功能将数据传回初始视图控制器。示例如下所示:

使用UINavigationControllerDelegate

class DetailsViewController: UIViewController, UINavigationControllerDelegate {
                                                        //     ^
    var data: [String] = []                             // Important!

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.delegate = self

        data = ["data has changed!"]
    }
}

Swift 2:

extension DetailsViewController: UINavigationControllerDelegate {
    func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
        if let controller = viewController as? ProgressTableViewController {
            controller.data = data    // Here you pass the data back to your original view controller
        }
    }
}

斯威夫特3:

extension DetailsViewController: UINavigationControllerDelegate {
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        (viewController as? ProgressTableViewController)?.data = data // Here you pass the to your original view controller
    }
}

在此示例中,密钥使用UINavigationControllerDelegate并设置导航控制器的委托(在本例中为self)。完成此操作后,您可以使用后退按钮将数据发送回初始视图控制器。

我个人更喜欢在我的数据中使用类:

为您的数据使用自定义类:

class Data {
    var array: [String] = []
}

进度视图控制器:

class ProgressTableViewController: UITableViewController {

    var data = Data()

    override func viewDidLoad() {
        super.viewDidLoad()

        data.array = ["some data"]
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        tableView.reloadData()
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()

        cell.textLabel?.text = data.array[indexPath.row]

        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "exerciseSegue", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "exerciseSegue" {
            let destination = segue.destinationViewController as! DetailsViewController
            destination.data = data
        }
    }
}

详细信息视图控制器:

class DetailsViewController: UIViewController {

    var data = Data()

    override func viewDidLoad() {
        super.viewDidLoad()

        data.array = ["data has changed!"]
    }
}

在上一个示例中,您不必担心传递数据。每当您更改数据时,使用相同类的控制器也会进行更改。

答案 1 :(得分:4)

通常,协议和委托用于在屏幕之间来回传递数据。

// Define a delegate that is known to both view controllers
protocol DetailsExerciseDelegate {
    func detailsWillDisappear(...);
}

class DetailsExerciseViewController {
    // Accept the delegate as a property on the details view controller
    var delegate : DetailsExerciseDelegate

    override func viewWillDisappear(animated : Bool) {
        super.viewWillDisappear(animated)

        // When you want to send data back to the caller
        // call the method on the delegate
        if let delegate = self.delegate {
            delegate.detailsWillDisappear(/* your data in one or more parameters */)
        }
    }
}

// Implement the delegate by adding the required function
class ProgressTableViewController: DetailsExerciseDelegate {
    ...

    func detailsWillDisappear(...) {
        // When the child calls the function, update the screen
        historyView.isFirstTime = false
        historyView.arrayData = arrayDataDetails
        historyView.arrayRipetizioni = arrayRipetizioniDetails
        historyView.arrayPeso = arrayPesoDetails
        historyView.arrayRecupero = arrayRecuperoDetails
        historyView.tableView.reloadData()
    }

    override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
        if segue!.identifier == "DetailsExcercise" {
            // And finally, when launching the child view,
            // make sure to set the delegate.
            let viewController = segue!.destinationViewController as DetailsExerciseViewController
            viewController.delegate = self
        }
    }
}

话虽如此,单击后退按钮时尝试保存数据似乎不合标准。您确定不想在“完成”中执行此操作吗?

答案 2 :(得分:2)

更简单的解决方案您可以使用self.navigationController?.viewControllers访问以前的视图控制器,如下所示:

let vcsCount = self.navigationController?.viewControllers.count
self.navigationController?.viewControllers[vcsCount! - 2].updateData

updateData是要在以前的视图控制器上更新的数据成员

答案 3 :(得分:0)

此外,您可以使用此扩展程序:

,