委托返回nil并且不被调用

时间:2018-11-23 12:01:37

标签: ios swift delegates overlay delegation

当单击button时,我想删除一个叠加层,从而关闭ViewController。我已调试,delegate当前返回nil。我不确定我在做什么错。我错过了实施其他项目吗?我什至尝试了打印语句,但什么也看不到。

protocol DismissOverlayDelegate: class {
    func dismissOverlay(_ sender: PlayersViewController)
}

class PlayersViewController: UIViewController {

    weak var delegate: DismissOverlayDelegate?

    @IBAction func getStartedTapped(_ sender: UIButton) {
        self.delegate?.dismissOverlay(self)
    }
}

在我的ViewController中实现delegate方法的地方

class HomeViewController: UIViewController, DismissOverlayDelegate {

    @IBOutlet weak var customOverlay: CustomOverlayView!

    let playersViewController = PlayersViewController()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.playersViewController.delegate = self
    }

    func dismissOverlay(_ sender: PlayersViewController) {
        self.customOverlay.removeFromView()
    }
}
  
    

delegate =(DismissOverlayDelegate?)无

  

PlayersViewController嵌入到UIPageViewController中

class HomeViewController: UIPageViewController, UIPageViewControllerDataSource {

    var pages = [UIViewController]()

    override func viewDidLoad() {
        super.viewDidLoad()
        for storyboardIDs in ["playersVC1","playersVC2"] {
            let viewController = self.storyboard?.instantiateViewController(withIdentifier: storyboardIDs)
            self.pages.append(viewController!)
        }
        self.dataSource = self
        self.setViewControllers([self.pages.first!], direction: .forward, animated: true)
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        let currentIndex = self.pages.firstIndex(of: viewController)!
        if currentIndex > 0 {
            return self.pages[currentIndex - 1]
        }
        return nil
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        let currentIndex = self.pages.firstIndex(of: viewController)!
        if currentIndex < (self.pages.count - 1) {
            return self.pages[currentIndex + 1]
        }
        return nil
    }
}

2 个答案:

答案 0 :(得分:0)

似乎PlayersViewController()是错误的。 使用StoryBoard时,应使用InstantiateInitialViewController()或InstantiateInitialViewController()。

https://developer.apple.com/documentation/uikit/uistoryboard/1616214-instantiateviewcontroller

https://developer.apple.com/documentation/uikit/uistoryboard/1616213-instantiateinitialviewcontroller

答案 1 :(得分:0)

如果您正在使用或想要使用委托进行调用或实现存在根的东西,请让我解释一下,您必须以正确的方式做到这一点。您做得很好,没问题,但是委托的实现不正确,您的方法无法接受,因此请尝试以下操作:-

您的主视图控制器

class HomeViewController: UIViewController, DismissOverlayDelegate {

    @IBOutlet weak var customOverlay: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func dismissOverlay(_ sender: PlayersViewController) {
        self.customOverlay.removeFromSuperview()
    }

    @IBAction func go(_ sender: UIButton) {
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "PlayersViewController") as! PlayersViewController
        vc.delegate = self
        self.navigationController?.pushViewController(vc, animated: true);
    }
}

这是您的游乐场VC

protocol DismissOverlayDelegate: class {
    func dismissOverlay(_ sender: PlayersViewController)
}

class PlayersViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    weak var delegate: DismissOverlayDelegate?

    @IBAction func getStartedTapped(_ sender: UIButton) {
        self.delegate?.dismissOverlay(self)
    }

}

现在希望您了解您必须做什么...!