iphone - 忽略 "UIViewController may not respond to [method]"警告的方法

标签 iphone ios objective-c cocoa-touch

有没有办法让编译器忽略这个特定的警告?

这就是我所做的:

UIViewController *firstViewController = AppDelegate.instance.viewController;

//open the view of the clicked subItem
if ([firstViewController respondsToSelector:@selector(openView:inView:)]) {
    [firstViewController openView:subItem.itemText.text inView:activeScreen]; //warning on this line
}

我知道一种可行的方法是将 UIViewController 更改为 ViewController (其类的名称)。但这个修复将来不会起作用,所以我只是在寻找一种方法来忽略这个警告。

它将来不会起作用,因为我会做这样的事情:

//.m

UIViewController *firstViewController;

//.h

if (someCondition) {
firstViewController = AppDelegate.instance.viewController;
}
else{
firstViewController = AppDelegate.instance.otherViewController;
}

if ([firstViewController respondsToSelector:@selector(openView:inView:)]) {
    [firstViewController openView:subItem.itemText.text inView:activeScreen]; //warning on this line
}

最佳答案

您应该在适当的情况下将对象转换为正确的类型。请注意,如果您愿意,您可以“转换”为协议(protocol)。 这使您可以安全地知道所需的方法已实现,而无需知 Prop 体类型。

如果您想让编译器不提示,可以通过调用performSelector:但是这样你就不会得到编译时检查。

[object performSelector:@selector(doSomething)]; 

查看讨论:Using -performSelector: vs. just calling the method

如果您想将一个对象传递给选择器,可以使用变体 performSelector:withObject:

如果要传递多个对象,则必须将它们包装在容器对象中,如 iOS - How to implement a performSelector with multiple arguments and with afterDelay? 中所述。 .

关于iphone - 忽略 "UIViewController may not respond to [method]"警告的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14651396/

相关文章:

iphone - iOS 5.0 模态视图不旋转( Storyboard)

ios - 如何在ios中获取paypal(1.4.3)的交易ID?

ios - 视频JS : not working on ipad

javascript - 我可以在 iPhone 上运行 javascript 运行时(如 v8)吗?

ios - 使用 resizableImageWithCapInsets 崩溃 :UIEdgeInsetsMake

iphone - 在 UI 自动化工具的脚本中,如何导入另一个脚本?

ios - UIView 组合不同时长的动画

objective-c - 具有可修改 AST 的 Objective C 解析器(如 C# 的 NRefactory)

objective-c - 在IDA Pro中,第二个ARM操作数中的 '::lower16::'是什么意思?

iphone - CAL层深度