Ios 创建多个按钮而不复制代码?
我知道有比我的noob实现更好的方法来实现这一点。我正在尝试将4个按钮添加到一个具有4个不同CGRECT的视图中,以便按钮一个叠在另一个上。代码现在运行良好,但我正在努力将此重复代码转化为一种工作方法:Ios 创建多个按钮而不复制代码?,ios,objective-c,Ios,Objective C,我知道有比我的noob实现更好的方法来实现这一点。我正在尝试将4个按钮添加到一个具有4个不同CGRECT的视图中,以便按钮一个叠在另一个上。代码现在运行良好,但我正在努力将此重复代码转化为一种工作方法: 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,
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];
我觉得这可以放进一个简单的方法,我可以调用四次,但不知道怎么做 每次重复某个内容时,请尝试循环
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];
}
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=@[第一次、第二次、第三次、第四次];
NSMutableArray*enableAlarmButtons=[NSMutableArray];//如果以后要访问这些按钮
对于(inti=0;i<4;++i){//可以使用sizeof来避免硬编码
UIButton*enableAlarm=[[UIButton alloc]initWithFrame:rects[i]];
[enableAlarm setTitle:titles[i]用于状态:UIControlStateNormal];
[enableAlarm setTitleColor:[UIColor blackColor]用于状态:UIControlStateNormal];
[[self view]addSubview:enableAlarm];
[enableAlarmButtons添加对象: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;
}
CGRect-rect=CGRectMake(30.0f、250.0f、250.0f、250.0f);
NSString*titles=@[第一次、第二次、第三次、第四次];
NSMutableArray*enableAlarmButtons=[NSMutableArray];//如果以后要访问这些按钮
对于(int i=0;i<4;++i){
UIButton*enableAlarm=[[UIButton alloc]initWithFrame:rects[i]];
[enableAlarm setTitle:titles[i]用于状态:UIControlStateNormal];
[enableAlarm setTitleColor:[UIColor blackColor]用于状态:UIControlStateNormal];
[[self view]addSubview:enableAlarm];
[enableAlarmButtons添加对象:enableAlarm];
矩形原点y+=25;
}
每次重复某些内容时,请尝试循环
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];
}
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=@[第一次、第二次、第三次、第四次];
NSMutableArray*enableAlarmButtons=[NSMutableArray];//如果以后要访问这些按钮
对于(inti=0;i<4;++i){//可以使用sizeof来避免硬编码
UIButton*enableAlarm=[[UIButton alloc]initWithFrame:rects[i]];
[enableAlarm setTitle:titles[i]用于状态:UIControlStateNormal];
[enableAlarm setTitleColor:[UIColor blackColor]用于状态:UIControlStateNormal];
[[self view]addSubview:enableAlarm];
[enableAlarmButtons添加对象: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;
}
CGRect-rect=CGRectMake(30.0f、250.0f、250.0f、250.0f);
NSString*titles=@[第一次、第二次、第三次、第四次];
NSMutableArray*enableAlarmButtons=[NSMutableArray];//如果以后要访问这些按钮
对于(int i=0;i<4;++i){
UIButton*enableAlarm=[[UIButton alloc]initWithFrame:rects[i]];
[enableAlarm setTitle:titles[i]用于状态:UIControlStateNormal];
[enableAlarm setTitleColor:[UIColor blackColor]用于状态:UIControlStateNormal];
[[self view]addSubview:enableAlarm];
[enableAlarmButtons添加对象:enableAlarm];
矩形原点y+=25;
}
如果您刚刚开始编程,那么找到一本好书或一系列在线教程可能比关于堆栈溢出的问题更好。看一看。《大书呆子牧场》的书非常好,很多人都喜欢iTunes U上的斯坦福iOS课程。祝你好运!如果您刚刚开始编程,那么找到一本好书或一系列在线教程可能比关于堆栈溢出的问题更好。看一看。《大书呆子牧场》的书非常好,很多人都喜欢iTunes U上的斯坦福iOS课程。祝你好运!比我快。请注意,标题也会更改(第一次
,第二次
,等等);你也需要一个数组或其他东西。另外,由于y值是矩形之间唯一的变化——并且是不断变化的——你可以通过在每次迭代中做加法来简化它。谢谢,Bryan。这很有帮助,也很有道理。快告诉我吧。请注意,标题也会更改(第一次
,第二次
,等等);你也需要一个数组或其他东西。另外,由于y值是矩形之间唯一的变化——并且是不断变化的——你可以通过在每次迭代中做加法来简化它。谢谢,Bryan。这非常有帮助,而且非常有意义。