【发布时间】: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