【问题标题】:Create multiple buttons without duplicating code?在不重复代码的情况下创建多个按钮?
【发布时间】:2014-10-21 15:44:45
【问题描述】:

我知道有比我的菜鸟实现更好的方法来做到这一点。我正在尝试将 4 个按钮添加到具有 4 个不同 CGrects 的视图中,以便按钮将一个堆叠在另一个之上。该代码现在可以正常工作,但我正在努力将这个重复的代码变成一种工作方法:

CGRect rect = CGRectMake(30.0f, 250.0f, 250.0f, 250.0f);
CGRect rect1 = CGRectMake(30.0f, 275.0f, 250.0f, 250.0f);
CGRect rect2 = CGRectMake(30.0f, 300.0f, 250.0f, 250.0f);
CGRect rect3 = CGRectMake(30.0f, 325.0f, 250.0f, 250.0f);

enableAlarm = [[UIButton alloc] initWithFrame:rect];
[enableAlarm setTitle:firstTime forState:UIControlStateNormal];
[enableAlarm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[[self view] addSubview:enableAlarm];

enableAlarm1 = [[UIButton alloc] initWithFrame:rect1];
[enableAlarm1 setTitle:secondTime forState:UIControlStateNormal];
[enableAlarm1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[[self view] addSubview:enableAlarm1];

enableAlarm2 = [[UIButton alloc] initWithFrame:rect2];
[enableAlarm2 setTitle:thirdTime forState:UIControlStateNormal];
[enableAlarm2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[[self view] addSubview:enableAlarm2];

enableAlarm3 = [[UIButton alloc] initWithFrame:rect3];
[enableAlarm3 setTitle:fourthTime forState:UIControlStateNormal];
[enableAlarm3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[[self view] addSubview:enableAlarm3];

我觉得这可以放在一个简单的方法中,我可以调用四次但不知道该怎么做......

【问题讨论】:

  • 如果您刚开始编程,那么找一本好书或一系列在线教程可能比 Stack Overflow 上的问题更能帮助您。看看Good resources for learning ObjC。 Big Nerd Ranch 的书籍非常棒,很多人喜欢 iTunes U 上的斯坦福 iOS 课程。祝你好运!

标签: ios objective-c


【解决方案1】:

每次你重复某件事时,尝试循环

CGRect rects = { {30.0f, 250.0f, 250.0f, 250.0f},
                {30.0f, 275.0f, 250.0f, 250.0f},
                {30.0f, 300.0f, 250.0f, 250.0f},
                {30.0f, 325.0f, 250.0f, 250.0f},
                };
NSString *titles = @[firstTime, secondTime, thirdTime, fourthTime];

NSMutableArray *enableAlarmButtons = [NSMutableArray array]; // if you want to access these buttons later
for (int i = 0; i < 4; ++i) { // you can use sizeof to avoid hardcode number
    UIButton  *enableAlarm = [[UIButton alloc] initWithFrame:rects[i]];
    [enableAlarm setTitle:titles[i]  forState:UIControlStateNormal];
    [enableAlarm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [[self view] addSubview:enableAlarm];
    [enableAlarmButtons addObject:enableAlarm];
}

如果只有 y 值发生变化

CGRect rect = CGRectMake(30.0f, 250.0f, 250.0f, 250.0f);
NSString *titles = @[firstTime, secondTime, thirdTime, fourthTime];

NSMutableArray *enableAlarmButtons = [NSMutableArray array]; // if you want to access these buttons later
for (int i = 0; i < 4; ++i) {
    UIButton  *enableAlarm = [[UIButton alloc] initWithFrame:rects[i]];
    [enableAlarm setTitle:titles[i]  forState:UIControlStateNormal];
    [enableAlarm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [[self view] addSubview:enableAlarm];
    [enableAlarmButtons addObject:enableAlarm];

    rect.origin.y += 25;
}

【讨论】:

  • 打败我。请注意,标题也会发生变化(firstTime、secondTime 等);您还需要一个数组或其他东西。
  • 另外,由于 y 值是矩形之间唯一的变化——并且不断变化——你可以通过在每次迭代中进行加法来简化它。
  • 谢谢,布莱恩。这非常有帮助,而且非常有意义。
猜你喜欢
  • 2020-01-15
  • 2010-10-18
  • 2021-12-14
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多