如何选择基于NSArray元素的方法(Objective-C)



我正在编写一种计算器应用程序。我有一个UIPickerView(1列)从字符串的NSArray加载数据。用户将选择其中一个(它选择使用哪种类型的计算器——每种计算器使用不同的计算方法)。用户输入一些东西到一些UITextFields,然后按一个UIButton来做计算。

我的NSArray是这样的

calcNames = [NSArray alloc] initWithObjects:@"first", @"second", @"third", nil];

我的方法叫做firstCalc(input1, input2, input3), secondCalc(input1, input2, input3),等等。(输入来自UITextFields)

当我按下按钮时,我想告诉它看看UIPickerView中的选择是什么,并运行相应的方法,而不只是为每个方法输入if-then语句(这是非常不方便的,因为我的应用程序特定的原因,这超出了这个讨论的范围)。

我已经定义了一种方法来确定所选的calc是什么:

selectedCalc = [[NSString alloc] initWithString:[calcNames objectAtIndex:row]]

其中'row'是UIPickerView中的当前选择。

现在我有一个doCalculations方法当有人按下UIButton时:

-(IBAction)doCalculations:(id)sender  {
    // save the data input
    double input1 = [input1Field.text doubleValue];
    double input2 = [input2Field.text doubleValue];
    double input3 = [input3Field.text doubleValue];
    // do the calculations
    int i;
    for (i = 0; i < [calcNames count]; i++)  {
        if (selectedCalc == [calcNames objectAtIndex:i])  {
            // do calculations here
            double numResult = ??????
            // if selectedCalc is "first", I want it to do firstCalc(input 1, input 2, input 3)
            // if selectedCalc is "second", I want it to do secondCalc(input 1, input 2, input 3), and so on
            // the rest is just for displaying the result
            NSString* result = [NSString stringWithFormat:@"The answer is %f", numResult];
            [resultLabel setText:result];
        }
    }
}

基本上,它运行一个for循环直到它找到从UIPickerView中选择的计算器当它找到它时,运行计算并显示它们

我一直在试图理解函数指针或选择器(NSSelectorFromString?)是正确的东西在这里使用和如何使用它们,但我真的很难理解在阅读苹果的文档,堆栈溢出问题,玩样例代码,并与我自己的代码修补几天后去哪里。

对不起,如果问题太长,我认为它可能会更有帮助,其他人寻求帮助,在未来看到完整的想法。(至少我知道有时候我被这些问题页搞糊涂了。)

我将非常感谢任何帮助,

瑞安

您可以使用选择器动态调用方法。例如,您可以使用calcSelectors选择器为calcNames设置二级数组:

SEL calcSelectors[] = (SEL[3]){ 
                               @selector(first:arg:), 
                               @selector(second:arg:), 
                               @selector(third:arg:)};

调用正确的方法就像这样简单:

[self performSelector:calcSelectors[calcIndex] withObject:arg1 withObject:arg2];

如果你需要多于2个参数,那么你还需要用NSInvocation实例来设置调用。

例1:

NSString *method=[calcNames objectAtIndex:0];//here play with objectatindex
SEL s=NSSelectorFromString(method);
[self performSelector:s];

which will call this method 
-(void)first{
    NSLog(@"first");
}

-----------------------------------------

示例2:

NSString *totalMethodName;
totalMethodName=@"vijay";
totalMethodName=[totalMethodName stringByAppendingString:@"With"];

totalMethodName=[totalMethodName stringByAppendingString:@"Apple"];

SEL s=NSSelectorFromString(totalMethodName);
[self performSelector:s];

will call 

-(void)vijayWithApple{
    NSLog(@"vijayWithApple called");
}

你可以使用NSInvocation动态地将多个参数绑定到一个选择器。跟着这篇文章学习吧。

如果你要使用NSInvocation你必须以objective-C的方式定义你的方法,就像下面这样。

- (double)firstCalcWithInput1:(double)input1 input2:(double)input2 andInput3:(double)input3;
- (double)secondCalcWithInput1:(double)input1 input2:(double)input2 andInput3:(double)input3;

最新更新