如何为 UINavigationController 派生对象创建初始化程序?

How to create an initializer for a UINavigationController derived object?

我有一个 class 继承自 UINavigationControllerUINavigationControllerDelegate

我在 viewDidLoad() 中设置委托:

self.navigationController?.delegate = self

然而,委托方法(例如 willShowdidShow)在将内容推送到堆栈时未被调用。

因此,在阅读过去的问题后,我试图在初始化程序中比 viewDidLoad 更早地设置委托。我已经尝试了一百万种组合,但它们要么无法编译,要么无法调用。

无法编译:

convenience init()
{
    super.init()
    self.navigationController?.delegate = self
}

无法编译:

convenience init()
{
    init(rootViewController: self)
    self.navigationController?.delegate = self
}

不会接到电话

convenience init()
{
    self.init()
    self.navigationController?.delegate = self
}

依此类推,根据编译器所说的进行尝试。但是编译器会说 "add self to call," 这样的东西,所以你添加它然后它会说 "remove self from call" 等等。我尝试了数百万种组合,尝试搜索过去的问题和 copy/paste 它们的初始值设定项。我仍然无法正常工作。

您需要使用 self.delegate = self,而不是 self.navigationController?.delegate = self,因为 selfUINavigationController

您正在尝试设置导航控制器的导航控制器的委托。但是您没有(也不能)将导航控制器放入另一个导航控制器中。