Swift/如何使用popViewController调用委托 [英] Swift / how to call delegate with popViewController

查看:73
本文介绍了Swift/如何使用popViewController调用委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读此线程(和从下到上),但完全不符合我的需求.

I have read this thread (and similar others) from bottom to top, but it doesn't fit my needs at all.

我在 UINavigationController UIPageViewController 内有一个 UIViewController .导航到第二个ViewController.导航到第3个ViewController,然后想弹出回到第2个ViewController,以传递数据.

I have a UIViewController inside UIPageViewController within a UINavigationController. Navigating to a 2nd ViewController. Navigating to a 3rd ViewController and want to pop back to 2nd ViewController delivering data.

我当前的代码:

protocol PassClubDelegate {
            func passClub(passedClub: Club)
        }

class My3rdVC: UIViewController {

        var clubs: [Club] = []

        var passClubDelegate: PassClubDelegate?

....

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let club = clubs[indexPath.row]
        self.passClubDelegate?.passClub(club)
        navigationController?.popViewControllerAnimated(true)
    }

我的第二个VC:

class My2ndVC: UIViewController, PassClubDelegate {

    var club = Club()

    func passClub(passedClub: Club) {

        SpeedLog.print("passClub called \(passedClub)")
        club = passedClub
    }

passClub 不被调用.我确定是因为我没有将委托设置为My2ndVC,但是我该怎么做呢?我找到的所有要我使用a)segue或b)实例化My2ndVC new的解决方案,这没有任何意义,因为它仍在内存中,并且我想弹出以返回到层次结构.我想念什么?我有什么可能?非常感谢您的帮助.

passClub is not called. I'm sure it's because I didn't set the delegate to the My2ndVC, but how would I do that? All the solutions I have found wanting me to use a) segue or b) instantiate a My2ndVC new, what doesn't make any sense since it's still in memory and I want to pop back to go back in hierarchy. What am I missing? What are my possibilities? Help is very appreciated.

PS :我没有使用任何命令.My3rdVC的调用者:

PS: I'm not using any segues. My3rdVC is called by:

let vc = stb.instantiateViewControllerWithIdentifier("My3rdVC") as! My3rdVC
self.navigationController?.pushViewController(vc, animated: true)

推荐答案

您可以在 My2ndVC prepareForSegue 方法中设置 My3rdVC 的委托>.

You can set the delegate of My3rdVC in the prepareForSegue method of My2ndVC.

class My2ndVC: UIViewController, PassClubDelegate {

    ...

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        super.prepareForSegue(segue, sender: sender)

        switch segue.destinationController {
        case let controller as My3rdVC:
            controller.passClubDelegate = self
        }
    }
}

这是假设您在情节提要中创建了一个序列,将 My3rdVC My2ndVC 推送到导航控制器堆栈中,我假设您已经拥有了.因此,只需尝试简单地将此 prepareForSegue 方法粘贴到 My2ndVC 中,并查看其是否有效.

This is assuming you have created a segue in your storyboard that pushes My3rdVC from My2ndVC onto the navigation controller stack, which I'm assuming you have. So just try simply pasting this prepareForSegue method into My2ndVC and see if it works.

更新

let vc = stb.instantiateViewControllerWithIdentifier("My3rdVC") as! My3rdVC

vc.passClubDelegate = self

navigationController?.pushViewController(vc, animated: true)

这篇关于Swift/如何使用popViewController调用委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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