如何根据 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 语句(它是由于我的应用程序特定的原因,执行此操作非常不方便,这超出了本讨论的范围)。
因此,我已经定义了一种方法来确定所选计算是什么:
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?)是否适合在这里使用以及如何使用它们,但在阅读了几天苹果的文档后,我真的很难理解该去哪里, Stack Overflow 问题、使用示例代码以及修改我自己的代码。
抱歉,如果问题太长,我认为对于将来寻求帮助的其他人来说,了解完整的想法可能会更有帮助。 (至少我知道有时我会迷失在这些问题页面中。)
我将非常感谢任何帮助,
Ryan
I'm writing a sort of calculator app. I have a UIPickerView (1 column) loading data from an NSArray of strings. The user will select one of these (it's selecting which type of calculator to use -- each uses a different method to calculate). The user inputs some things into some UITextFields and then presses a UIButton to do the calculations.
My NSArray is this:
calcNames = [NSArray alloc] initWithObjects:@"first", @"second", @"third", nil];
And my methods are called firstCalc(input1, input2, input3), secondCalc(input1, input2, input3), and so on. (The inputs are coming from the UITextFields.)
When I press the button, I would like to tell it to look at what the selection in the UIPickerView is and run the corresponding method without just typing an if-then statement for each one (it's very inconvenient to do this for reasons specific to my app, which are beyond the scope of this discussion).
So I have already defined a way to determine what the selected calc is:
selectedCalc = [[NSString alloc] initWithString:[calcNames objectAtIndex:row]]
where 'row' is the current selection in the UIPickerView.
Now I have a doCalculations method for when someone presses the 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];
}
}
}
So basically, it runs a for loop until it finds which calculator is selected from the UIPickerView and when it finds it, runs the calculations and displays them.
I've been trying to understand if maybe function pointers or selectors (NSSelectorFromString?) are the right things to use here and how to use them, but I'm really struggling to understand where to go after a couple days of reading Apple's documentation, Stack Overflow questions, playing with sample code, and tinkering with my own code.
Sorry if the question is too lengthy, I thought it may be more helpful to others looking for assistance in the future to see the full idea. (At least I know sometimes I'm lost with these question pages.)
I would be very grateful for any assistance,
Ryan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用选择器动态调用方法。例如,您可以有一个
calcNames
的辅助数组,其中选择器名为calcSelectors
:调用正确的方法将非常简单:
如果您需要两个以上的参数,那么您还需要稍微修改一下
NSInitation
实例来设置调用。You can dynamically invoke a method using a selector. You could for example have a secondary array to
calcNames
with selector calledcalcSelectors
:Calling the right method would then be as simple as:
If you need more then 2 arguments, then you also need to mess a bit with a
NSInvocation
instance to setup the call.示例 1:
示例 2:
Example 1:
Example 2:
您可以使用NSInitation将多个参数动态绑定到选择器。 按照这篇文章学习。
如果您要使用NSInitation,您必须以 Objective-C 方式定义您的方法,如下所示。
You can make use of NSInvocation to dynamically bind multiple arguments to a selector. Follow this post to learn it.
If you are going to use NSInvocation you have to define your methods in the objective-C way something like the following.