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

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

问题描述

我处于这种情况:

我使用prepare for segue将4个数组从进度表传递到详细练习,它工作正常!当我尝试将数据从详细练习控制器传递回进度表控制器时,问题就开始了.我想使用默认的导航返回按钮返回父视图.实际上我正在使用此代码但它不起作用,数据从子视图传递到父视图但我在进度表中看不到结果,似乎我需要刷新但我也尝试了 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:

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

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.delegate = self

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

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

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天全站免登陆