ios - 使用 UIViewController 子类作为参数设置按钮目标

标签 ios swift

我有BaseViewController,它是UIViewController的子类,然后我有这个LeftViewControllerRightViewController,它们是BaseViewController 的子类。我想创建一个函数来创建从 BaseViewController 声明的导航栏按钮项,以便我可以从子级访问。

问题是,为了设置每个 navbaritem 的操作/选择器,如何将 subview Controller 作为参数传递给该函数?有些选择器只是在 LeftViewController 中声明,或者仅在 RightViewController 中声明,因此编译器会提示未声明的选择器。

谢谢。

最佳答案

在BaseviewController选择器方法中,您可以使用关键字self来获取Childviewcontroller,因为应用了运行时多态性,您可以使用self作为

if self.isKindOfClass(RightViewController.classForCoder()) {
    let controller: RightViewController = self as! RightViewController
}

关于ios - 使用 UIViewController 子类作为参数设置按钮目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37206693/

相关文章:

objective-c - 使用 CHCSVWriter 保存 CSV 文件

ios - iBeacon监控无法正常工作

ios - 设置允许不同大小的应用程序范围的默认字体

ios - 未找到框架 Pod

ios - 将repeatCount设置为1e100f会发出警告

ios - 如何以编程方式将用户名和密码存储在自动填充中?

Swift AVPlayer 在模拟器上抛出异常

ios - 当调用未连接但调用套件ui在后台运行时如何关闭调用套件

ios - (SwiftUI) 如何在 View 加载时隐藏文本并在点击切换后变得可见

ios - Swift 函数在应用程序中有效,但在 override func viewDidLoad() 中无效