在我看来,使用 InterfaceBuilder 并不容易。您最好使用代码创建它们。对于分页,您必须使用分页 UIScrollView。
#define BUTTON_WIDTH 40
#define BUTTON_HEIGHT 30
#define BUTTON_PADDING 10
#define BUTTONS_PER_PAGE 6
#define PAGE_WIDTH ((BUTTON_WIDTH + BUTTON_PADDING) * BUTTONS_PER_PAGE)
#define MAGIC_BUTTON_TAG_OFFSET 6238 /* Maybe somebody already uses views with tags like 0, 1, 2, 3...)
// in an init... method:
UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, 320, 320)];
sv.contentSize = CGSizeMake(PAGE_WIDTH, sv.frame.size.height);
sv.pagingEnabled = YES;
[self.view addSubview:sv]; // assuming a view controller
NSArray *a = [NSArray arrayWithObjects:...]; // whatever
for (int i = 0; i < a.count; i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(i * (BUTTON_WIDTH + BUTTON_PADDING), 0, BUTTON_WIDTH, BUTTON_HEIGHT);
btn.tag = i + MAGIC_BUTTON_TAG_OFFSET; // to relate to the array index
[btn setTitle:[NSString stringWithFormat:@"Button No. %d", i] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[sv addSubview:btn];
}
- (void)buttonPressed:(UIButton *)btn
{
int index = btn.tag - MAGIC_BUTTON_TAG_OFFSET;
id object = [a objectAtIndex:index];
[self doSomethingWithObject:object];
}
希望这可以帮助。