Objective-C 调用编译器不相信存在的选择器(即使我们知道它存在) [英] Objective-C Calling a selector that the compiler does not believe exists (even though we know it does)

查看:53
本文介绍了Objective-C 调用编译器不相信存在的选择器(即使我们知道它存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 prepareForSegue 方法中有这个代码

I have this code in a prepareForSegue method

    // Get destination view
    UIViewController *viewController = [segue destinationViewController];

    //See if it responds to a selector
    if ([viewController respondsToSelector:@selector(setSomethingOrOther:)]) {
        //if so call it with some data
        [viewController setSomethingOrOther:something];
    }

上面的代码意味着我不必包含对要转入的视图控制器的实际类的引用.我可以更松散地耦合两个视图控制器,只需检查它是否响应设置在其上的某些属性.

The code above means I do not have to include a reference to the actual class of the view controller being segue'd to. I can more loosely couple the two view controllers and just check if it responds to some property being set on it.

问题是,当我这样做时,我得到以下编译时错误:

The problem is that when I do this I get the following compile time error:

UIViewController"没有可见的@interface 声明了选择器setSomethingOrOther:"

这当然是真的.我知道我可以通过包含对视图的引用来解决它,但我更愿意将它分开.我该如何解决这个问题

which is true of course. I know I could get around it by including a reference to the view but I would prefer to keep it separated. How can I work around this

推荐答案

使用 performSelector:aSelector 方法,就可以调用未声明的选择器.

Use the performSelector:aSelector method, then you can call an undeclared selector.

这篇关于Objective-C 调用编译器不相信存在的选择器(即使我们知道它存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆