Ios Swift 3:返回到发送数据的最后一个ViewController

Ios Swift 3:返回到发送数据的最后一个ViewController,ios,swift,uiviewcontroller,poptoviewcontroller,Ios,Swift,Uiviewcontroller,Poptoviewcontroller,我试图返回我的las viewController并发送数据,但它不起作用 当我只使用popViewController时,我可以返回页面,但不能将数据从B移到A 这是我的密码: func goToLastViewController() { let vc = self.navigationController?.viewControllers[4] as! OnaylarimTableViewController vc.onayCode.userId = taskInfo.us

我试图返回我的las viewController并发送数据,但它不起作用

当我只使用popViewController时,我可以返回页面,但不能将数据从B移到A

这是我的密码:

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

要将数据从子控制器传递到父控制器,必须使用委托模式传递数据

实现委派模式的步骤,假设A是父viewController,B是子viewController

  • 创建协议,并在B中创建委托变量

  • 扩展协议

  • 当按下或显示viewcontroller时,将引用传递到A的B

  • 在接收操作中定义委托方法

  • 之后,根据您的条件,您可以从B调用委托方法


  • 您应该使用
    委托协议

    class MyClass: NSUserNotificationCenterDelegate
    
    具体实施如下:

    func userDidSomeAction() {
        //implementation
    }
    
    当然,您必须在父类中实现deleget,比如

    childView.delegate = self
    
    查看此项了解更多信息

    您必须使用2个选项发送回上一个ViewController

    1.放松赛格。(使用情节提要)
    您可以参考

    2.使用委托/协议
    您可以参考

    也将对您有用。

    您可以使用

    例如,我有两个屏幕。第一个显示关于用户的信息,从那里,他进入屏幕选择他的城市。有关更改城市的信息应显示在第一个屏幕上

    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 ?? "")"
        }
    }
    
    和协调员:

    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
            }
        }
    }
    
    coordinator = UserEditCoordinator(user: user, navigationController: navigationController)
    coordinator.start()
    
    然后我们只需要启动协调器:

    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
            }
        }
    }
    
    coordinator = UserEditCoordinator(user: user, navigationController: navigationController)
    coordinator.start()
    

    如果这是你以前的VC,你可以使用上面的代码。你想把数据发送回根视图控制器吗?