ios - addTarget parentViewController 和 xcode 警告我,我的选择器未声明

标签 ios objective-c xcode

它比真正的问题更令人好奇,因为它有效。但我不明白 xcode 警告。
在我的应用程序中,我使用 addChildViewController 所以我有一个 child 和一个 parent 。在我的 child 中,我有一个 UIButton,我想为 parent 的功能设置事件:它可以工作,但 xcode 说未声明的选择器 parentFunction:/
我可以声明这个函数来消除这个警告,但它会是死代码(不好的方式)。
或者我可以使用委托(delegate),但是当您有直接链接时为什么要使用委托(delegate)。

[btn addTarget:self.parentViewController action:@selector(parentFunction:) forControlEvents:UIControlEventTouchUpInside];    //undeclared selector parentFunction

最好的祝福

谢谢

最佳答案

您需要导入父 View Controller 的 .h 文件,并将 self.parentViewController 转换为该类,否则编译器不知道该方法存在(因为 parentViewController 返回 UIViewController,而不是您的自定义 Controller )。

[btn addTarget:(ParentClass *)self.parentViewController action:@selector(parentFunction:) forControlEvents:UIControlEventTouchUpInside];

将 ParentClass 替换为该父级的任何类名。此外,请确保 parentFunction: 在父类中实现(并且有冒号),并在其 .h 文件中声明。

关于ios - addTarget parentViewController 和 xcode 警告我,我的选择器未声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20306930/

相关文章:

ios - Xcode @IBDesignable 按钮背景颜色未在 Storyboard 中呈现

ios - iOS:在哪里下载uikit和基础框架

ios - SimplePCMRecorder 中的 AudioQueueBufferRef 在 swift 3 中显示错误

ios - 如何在 iOS 9 中获取 iphone 设备 token

ios - 在 iOS 13 中的图像上绘制文本

ios - FBFinalClassViolationException : FBAdTimer is a final class and cannot be subclassed

ios - 使用缓存异步加载本地 UIImage

ios - UISwipeGesture 不在监听 touchesBegan 的 View 上工作

ios - 如何对键的 NSArray 进行排序?

ios - 这可以在 iOS 上处理 302 重定向之前的通用链接吗?