0

我希望能够用一个功能更新两组相同的按钮。此外,我不想更新所有按钮,只更新其中一些按钮。

我可以有这样的功能吗?:

-(void) updateFields{
                    updateButton1 : (Bool) x
                    updateButton2 : (Bool) y
                    updateButton3 : (Bool) z }

实现将如下所示:

[button1_1 setEnabled:x];
[button1_2 setEnabled:x]; //called only if updateButton1 is given an argument
[button2_1 setEnabled:y];
etc...
4

4 回答 4

0

传递一个按钮数组和一个包裹在 NSNumber 中的布尔数组怎么样?

- (void)updateButton:(NSArray *)buttons withArray:(NSArray *)enablers {

    // buttons is an array of UIButton
    // enablers is an array of NSNumber created from boolean value

    // Security check
    if(buttons.count != enabler.count) {
        NSLog(@"Error: array have different dimensions");
        return;
    }        

    // Enable buttons
    for(int i=0; i<buttons.count; i++) {
        UIButton *button = (UIButton *)[buttons objectAtIndex:i];
        BOOL enable = [[enablers objectAtIndex:i] boolValue]

        [button setEnabled:enable];
    }
}
于 2013-10-24T15:37:01.557 回答
0

It's possible to create an Objective-C method with a variable argument list, as Matt Gallagher explains in Variable argument lists in Cocoa. Variable argument lists are even used in the Foundation framework, e.g. +[NSArray arrayWithObjects:...].

That said, it's probably a lot less work to pass the list of buttons in your method as an array, particularly given the ease with which one can now create arrays using object literals:

[foo updateFields:@[button1, button2, button3]];
于 2013-10-24T15:50:40.373 回答
0

这对于原始数据类型可能是不可能的,除非您从它们创建对象并将它们放在 NSArray 或 NSDictionary 中。其他选项可以是创建自定义对象并将其作为参数传递。

- (void)selectButton:(SelectedButton *)iButton {
     if (iButton.type = A) {
     // Handle A
    } else if (iButton.type = B) {
     // Handle B
    } else if (iButton.type = C) {
     // Handle C
    }
}
于 2013-10-24T15:43:02.473 回答
0

我认为您要使用的语法作为 C 函数更有意义

但是请注意,在此示例中,参数不是可选的。

void updateButtons(BOOL btn1, BOOL btn2, BOOL btn3){
    button1.enabled = btn1
    button2.enabled = btn2
    button3.enabled = btn3
}
于 2013-10-24T15:43:02.637 回答