通过导航后退按钮传递数据 [英] Pass data through navigation back button

查看:78
本文介绍了通过导航后退按钮传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这种情况:

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

I am passing 4 array from Progress Table to Detail Exercise using prepare for segue and it works fine! The problem begin when I try to pass the data back from the Detail Exercise Controller to the Progress Table Controller. I would like to use the default navigation back button to go back to the parent view. Actually I'm using this code but it doesn't work, well the data pass from the child to the parent view but i can't see the result in the Progress Table, seems like i need to refresh but i also tryed the reloadData after the viewDidLoad and it doesn't work. Any suggestion? Thank you.

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


推荐答案

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

When you press the back button, the navigation controller will call navigationController(willShowViewController:) so you can use this to pass the data back to your initial view controller. An example is shown below:

使用 UINavigationControllerDelegate

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

Swift 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 )。完成此操作后,您可以使用后退按钮将数据发送回初始视图控制器。

In this example, the key is using the UINavigationControllerDelegate and setting the delegate of the navigation controller (in this case it's self). Having done this, you can send the data back to your initial view controller with the back button.

我个人更喜欢使用类来获取数据:

Personally I prefer using a class for my data:

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

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!"]
    }
}

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

In the last example, you don't have to worry about passing around data. Whenever you change the data, the controllers using the same class, will have the changes as well.

这篇关于通过导航后退按钮传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆