ios - 如何在字符串中动态地从另一个类调用方法?

时间:2013-06-05 09:08:26

标签: iphone ios objective-c

我是iPhone开发的新手,我想从String

中的另一个类调用一个方法

在LoginSuccess.m中

- (void)Post:(NSString *)url AndSecondStr:(NSString *)postData AndThirdStr:(NSString *)contentType

在Register.m

LoginSuccess *loginViewInstance =  [[LoginSuccess alloc]initWithNibName:@"LoginSuccess" bundle:nil];
[loginViewInstance Post:TxtEmailId AndSecondStr:TxtUserName AndThirdStr:serializeObject];
执行上面的

,但我想在String中调用该方法,如

NSString *result = [loginViewInstance Post:TxtEmailId AndSecondStr:TxtUserName AndThirdStr:serializeObject];

有什么想法吗?提前谢谢。

2 个答案:

答案 0 :(得分:7)

替换

- (void)Post:(NSString *)url AndSecondStr:(NSString *)postData AndThirdStr:(NSString *)contentType

- (NSString *)Post:(NSString *)url AndSecondStr:(NSString *)postData AndThirdStr:(NSString *)contentType
{
    NSString *str;
    str = someValue;
    return str;
}

答案 1 :(得分:0)

您的返回类型数据是(void)。请先用NSString替换它,如:

(NSString *)Post:(NSString *)url AndSecondStr:(NSString *)postData AndThirdStr:(NSString *)contentType
{
//do something
}

然后从另一个类调用一个方法就可以了 比如先在Register.m中添加这段代码。

#import "LoginSuccess.h"

然后将其称为:

 LoginSuccess *Call_String = (LoginSuccess *)[LoginSuccess sharedApplication];

 NSString *result = [Call_String Post:TxtEmailId AndSecondStr:TxtUserName AndThirdStr:serializeObject];   

希望你的问题能够得到解决