Objective C用一个字符串动态调用方法 [英] Objective C calling method dynamically with a string

查看:108
本文介绍了Objective C用一个字符串动态调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道是否有一种方法来调用一个方法,我用一个字符串来构建方法的名称。

Im just wondering whether there is a way to call a method where i build the name of the method on the fly with a string.

我有一个方法称为loaddata

e.g. I have a method called loaddata

-(void)loadData;

调用这个我通常会像

[self loadData];

但是我想用一个字符串动态地调用它,例如

But i want to be able to call it dynamically with a string e.g.

NSString *methodName = [[NSString alloc] initWithString:@"loadData"];
[self methodName];

这是一个愚蠢的例子,但我希望你得到我的观点。我正在使用它来为我的IPad应用程序设置的数据绑定类。很难解释,但要得到它,我需要解决如何调用一个字符串的方法。

This is a stupid example but i hope you get my point. I am using it for databinding classes that I am setting up for my IPad application. Hard to explain but to get it to fire I need to work out how to call a method with a string.

任何想法?

谢谢

推荐答案

你可以尝试像

SEL s = NSSelectorFromString(selectorName);
[anObject performSelector:s];

这篇关于Objective C用一个字符串动态调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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