1

I'm trying to go back to my las viewController with sending data, but it doesn't work.

When I just use popViewController, I can go back to the page, but I can't move my datas from B to A.

Here is my code :

func goToLastViewController() {
    let vc = self.navigationController?.viewControllers[4] as! OnaylarimTableViewController
    vc.onayCode.userId = taskInfo.userId
    vc.onayCode.systemCode = taskInfo.systemCode

    self.navigationController?.popToViewController(vc, animated: true)
}
jorjj
  • 1,479
  • 4
  • 20
  • 36

4 Answers4

1

To pass data from Child to parent Controller, you have to pass data using Delegate pattern.

Steps to implement delegation pattern, Suppose A is Parent viewController and B is Child viewController.

  1. Create protocol, and create delegate variable in B

  2. Extend protocol in A

  3. pass reference to B of A when Push or Present viewcontroller

  4. Define delegate Method in A, receive action.

  5. After that, According to your condition you can call delegate method from B.

Jaydeep Vora
  • 6,085
  • 1
  • 22
  • 40
0

You should do it using delegate protocol

class MyClass: NSUserNotificationCenterDelegate

The implementation will be like following:

func userDidSomeAction() {
    //implementation
}

And ofcourse you have to implement delegete in your parent class like

childView.delegate = self

Check this for more information https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html

Paras Gorasiya
  • 1,295
  • 2
  • 13
  • 33
0

You have to send back to last ViewController with 2 options.

1. Unwind segue. (With use of storyboard)
You can refer this link.

2. Use of delegate/protocol.
You can refer this link.

Also this link will be useful for you.

Shah Nilay
  • 778
  • 3
  • 21
0

You can use Coordinator Pattern

For example, I have 2 screens. The first displays information about the user, and from there, he goes to the screen for selecting his city. Information about the changed city should be displayed on the first screen.

final class CitiesViewController: UITableViewController {
    // MARK: - Output -
    var onCitySelected: ((City) -> Void)?

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        onCitySelected?(cities[indexPath.row])
    }
    ...
}

UserEditViewController:

final class UserEditViewController: UIViewController, UpdateableWithUser {
    // MARK: - Input -
    var user: User? { didSet { updateView() } }

    @IBOutlet private weak var userLabel: UILabel?

    private func updateView() {
        userLabel?.text = "User: \(user?.name ?? ""), \n"
                        + "City: \(user?.city?.name ?? "")"
    }
}

And Coordinator:

protocol UpdateableWithUser: class {
    var user: User? { get set }
}

final class UserEditCoordinator {

    // MARK: - Properties
    private var user: User { didSet { updateInterfaces() } }
    private weak var navigationController: UINavigationController?

    // MARK: - Init
    init(user: User, navigationController: UINavigationController) {
        self.user = user
        self.navigationController = navigationController
    }

    func start() {
        showUserEditScreen()
    }

    // MARK: - Private implementation
    private func showUserEditScreen() {
        let controller = UIStoryboard.makeUserEditController()
        controller.user = user
        controller.onSelectCity = { [weak self] in
            self?.showCitiesScreen()
        }
        navigationController?.pushViewController(controller, animated: false)
    }

    private func showCitiesScreen() {
        let controller = UIStoryboard.makeCitiesController()
        controller.onCitySelected = { [weak self] city in
            self?.user.city = city
            _ = self?.navigationController?.popViewController(animated: true)
        }
        navigationController?.pushViewController(controller, animated: true)
    }

    private func updateInterfaces() {
        navigationController?.viewControllers.forEach {
            ($0 as? UpdateableWithUser)?.user = user
        }
    }
}

Then we just need to start coordinator:

coordinator = UserEditCoordinator(user: user, navigationController: navigationController)
coordinator.start()