gpt4 book ai didi

ios - 委托(delegate)返回 nil 且未被调用

转载 作者:搜寻专家 更新时间:2023-10-31 23:06:24 25 4
gpt4 key购买 nike

我有一个叠加层,我想在单击 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?) nil

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

最佳答案

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

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

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

关于ios - 委托(delegate)返回 nil 且未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53446362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com