Это очень просто. Я использую gridView в качестве класса UiViewController и добавляю следующий код в gridView.m
#import "SudokuClass.h"
#import "GridView.h"
@interface GridView ()
@end
@implementation GridView
-(void)createNumberButton {
int cellWidth = 34;
int cellHeight = 34;
int xSta = 7 - cellWidth;
int ySta = 50 - cellHeight;
//NSMutableDictionary *buttonTable = [[NSMutableDictionary alloc] initWithCapacity:81];
for (int i = 1; i < 10; i++) {
xSta = xSta + cellWidth;
for (int j = 1 ; j < 10; j++) {
ySta = ySta + cellHeight;
CGRect pos = CGRectMake(xSta, ySta, cellWidth, cellHeight);
UIButton *b = [SudokuClass createSudokuButtonForView:self atPos:pos
withTag:j*10+i forAction:@selector(numButtonPressed:)];
NSString *picName = @"ButtonPic.jpg";
[b setBackgroundImage:[[UIImage imageNamed:picName] stretchableImageWithLeftCapWidth:0 topCapHeight:0] forState:0];
[self.view addSubview:b];
//[numButtons addObject:b];
}
ySta = 50 - cellHeight;
}}
-(void)viewDidLoad
{
[self createNumberButton];
[super viewDidLoad];
}
@end
здесь класс sudoku - это мой класс nsobject с методом
+(UIButton *)createSudokuButtonForView:(UIViewController *)view atPos:(CGRect)position withTag:(int)tag forAction:(SEL)action {
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setFrame:position];
[b.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
[b setTag:tag];
[b addTarget:view action:action forControlEvents:UIControlEventTouchDown];
[b setTitle:@"" forState:0];
[b setTitleColor:[UIColor blackColor] forState:0];
//b.layer.frame = CGRectMake(xSta-1, ySta-1, 31, 31);
//[b.layer setBorderWidth:borderWidth];
b.userInteractionEnabled = YES;
return b;}
надеюсь, кто-нибудь найдет это полезным ..:)