如何为每个按钮设置标题,以便将按钮标题设置为:1,2,3,4 5等按钮?
我的按钮:
@interface PregnancyViewController () {
NSMutableArray *buttons;
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
for( int i = 0; i < 5; i++ ) {
for( int j = 0; j < 6; j++ ) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(j * 50 + 10, i * 50 + 20, 40, 40);
// Add buttons to my mutable array
[buttons addObject: button];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
}
答案 0 :(得分:3)
这个怎么样:
- (void)viewDidLoad
{
[super viewDidLoad];
NSUInteger k = 1;
for( NSUInteger i = 0; i < 5; i++ ) {
for( NSUInteger j = 0; j < 6; j++ ) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(j * 50 + 10, i * 50 + 20, 40, 40);
NSString* title = [NSString stringWithFormat: @"%@", @(k++)];
[button setTitle: title forState: UIControlStateNormal];
// Add buttons to my mutable array
[buttons addObject: button];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
}