【问题标题】:How to choose a method based on an element of an NSArray (Objective-C)如何根据 NSArray 的元素选择方法(Objective-C)
【发布时间】:2011-10-17 08:19:47
【问题描述】:

我正在编写一种计算器应用程序。我有一个 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 语句(由于原因,这样做非常不方便特定于我的应用程序,超出了本次讨论的范围)。

所以我已经定义了一种方法来确定所选计算是什么:

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

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

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

-(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?)是否适合在这里使用以及如何使用它们,但经过几天的阅读,我真的很难理解该去哪里Apple 的文档、Stack Overflow 问题、使用示例代码以及修改我自己的代码。

对不起,如果问题太长,我认为它可能对其他人在未来寻求帮助以查看完整的想法更有帮助。 (至少我知道有时我会迷失在这些问题页面上。)

如果有任何帮助,我将非常感谢,

瑞恩

【问题讨论】:

    标签: objective-c ios methods nsstring nsarray


    【解决方案1】:

    您可以利用 NSInvocation 将多个参数动态绑定到选择器。 Follow this post to learn it.

    如果您要使用 NSInvocation,您必须以 Objective-C 方式定义您的方法,如下所示。

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

    【讨论】:

      【解决方案2】:

      您可以使用选择器动态调用方法。例如,您可以使用名为 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 实例来设置调用。

      【讨论】:

        【解决方案3】:

        示例 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");
        }
        

        【讨论】:

          猜你喜欢
          • 2012-06-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-20
          • 1970-01-01
          • 1970-01-01
          • 2010-10-01
          • 2019-10-15
          相关资源
          最近更新 更多