gpt4 book ai didi

ios - 我正在尝试快速使用自定义委托(delegate),但是当我想调用它的方法时,它没有被调用

转载 作者:行者123 更新时间:2023-11-28 07:52:57 25 4
gpt4 key购买 nike

我一直在寻找委托(delegate)的工作原理,并尝试在我的项目中这样做。不幸的是,我实现的委托(delegate)方法从未被调用过。我正在尝试做一个滑出式导航面板。所以我所做的是放置两个 uicontainerview,一个用于滑出式导航面板,另一个用于主视图 Controller enter image description here

代码是这样的对于主视图 Controller

protocol MainViewControllerDelegate {
func toggleSideMenu()
}

class MainViewController: UIViewController {

var delegate: MainViewControllerDelegate?

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

// MARK: - Slide Action

@IBAction func slideMenuTapped(_ sender: UIBarButtonItem){
delegate?.toggleSideMenu()
print("Slide Menu has been tapped")
}
}

对于容器 View Controller

class ContainerVC: UIViewController {
@IBOutlet weak var SideMenuConstraint: NSLayoutConstraint!
@IBOutlet weak var slideMenuContainer: UIView!
@IBOutlet weak var mainViewContainer: UIView!

var mainViewController: MainViewController?

var isSideMenuOpened = false

override func viewDidLoad() {
super.viewDidLoad()

mainViewController = UIStoryboard.mainViewController()
mainViewController?.delegate = self
}
}

extension ContainerVC: MainViewControllerDelegate{
func toggleSideMenu() {
print("It works")
if isSideMenuOpened{
isSideMenuOpened = false
SideMenuConstraint.constant = -260
mainViewContainer.layer.shadowOpacity = 0
} else {
isSideMenuOpened = true
SideMenuConstraint.constant = 0
mainViewContainer.layer.shadowOpacity = 0.59
}
UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded()
}
}
}

extension UIStoryboard{
static func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: Bundle.main) }
static func mainViewController() -> MainViewController? {
return mainStoryboard().instantiateViewController(withIdentifier: "MainViewController") as? MainViewController
}
}

有什么问题请告知

最佳答案

我认为原因是您将主视图 Controller 嵌入到导航 Controller 中:

let navigationController = self.childViewControllers.last as! UINavigationController
let mainViewController = navigationController.topViewController as! MainViewController
mainViewController?.delegate = self

关于ios - 我正在尝试快速使用自定义委托(delegate),但是当我想调用它的方法时,它没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49217858/

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