在Objective-C中循环对象

在Objective-C中循环对象,objective-c,Objective C,我有许多ui按钮s,我正在以编程方式为它们设置自定义字体,如下所示: Button1.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16]; Button2.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16]; Button3.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:1

我有许多
ui按钮
s,我正在以编程方式为它们设置自定义字体,如下所示:

Button1.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button2.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button3.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button4.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button5.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button6.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
for (int i = 1; i <= 5; i++) {
    Button(i).titleLabel.font = etc.. // How would I get the value of 'i'?
}
NSMutableArray *buttons = [NSMutableArray array];
[buttons addObject: Button1];
[buttons addObject: Button2];
[buttons addObject: Button3];
[buttons addObject: Button4];
[buttons addObject: Button5];
[buttons addObject: Button6];

for (UIButton *button in buttons) {
    button.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
}
这段代码是正确的,它可以工作,但它似乎不是最简洁的编写方式。有没有办法在这些元素之间循环

我曾经想象过这样的事情:

Button1.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button2.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button3.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button4.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button5.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button6.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
for (int i = 1; i <= 5; i++) {
    Button(i).titleLabel.font = etc.. // How would I get the value of 'i'?
}
NSMutableArray *buttons = [NSMutableArray array];
[buttons addObject: Button1];
[buttons addObject: Button2];
[buttons addObject: Button3];
[buttons addObject: Button4];
[buttons addObject: Button5];
[buttons addObject: Button6];

for (UIButton *button in buttons) {
    button.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
}

对于(int i=1;i您可以使用NSArray来迭代:

UIFont *font = [UIFont fontWithName:@"myCustomFont" size:16];
NSArray *buttons = [NSArray arrayWithObjects: Button1, Button2, Button3, 
                                              Button4, Button5, Button6, 
                                              nil];
for (UIButton *button in buttons) {
  button.titleLabel.font = font;
}

嗯,我想你可以把所有的按钮放在一个数组中,然后快速枚举


NSArray *buttons; 
//Put all of your buttons inhere
for (UIButton *button in buttons)
{
    button.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
}
您可以这样做:

Button1.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button2.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button3.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button4.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button5.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
Button6.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
for (int i = 1; i <= 5; i++) {
    Button(i).titleLabel.font = etc.. // How would I get the value of 'i'?
}
NSMutableArray *buttons = [NSMutableArray array];
[buttons addObject: Button1];
[buttons addObject: Button2];
[buttons addObject: Button3];
[buttons addObject: Button4];
[buttons addObject: Button5];
[buttons addObject: Button6];

for (UIButton *button in buttons) {
    button.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
}


for(inti=1;i这个怎么样?currentView是包含所有按钮的UIView

NSArray* buttonsArray = currentView.subviews;

for((UIButton*) button in buttonsArray)
{
     button.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
}

按钮是在一个数组中还是在单个实例中?您需要多久执行一次(在多少个不同的位置)?您可以将它们添加到NSdictionary中,并循环通过以将它们拉出并更改属性,但如果替换6行代码,则几乎不值得。只有当按钮在某种集合中的顺序已知时,循环通过才有意义,这种集合非常整洁。遗憾的是,我只能选择一个正确的答案!我选择不要忘记
[NSArray arrayWithObjects:]的最后一个参数
nil
以避免未定义的行为。这是一个所谓的可变参数列表,需要
nil
来确定没有更多参数。您不能将对象添加到
NSArray
,因为它是不可变的(在init之后不能修改)。但您可以使用
NSMutableArray
来实现这一点。