我想创建一个带有Field的国际象棋游戏。在代码中我首先创建了Chessboard via Loop。
-(void)CreateGame{
NSMutableArray* Board = [[NSMutableArray alloc]init];
for (int x = 1; x <= 9; x++) {
for (int y = 1; y <= 9; y++) {
MainGame* Boardfield = [[MainGame alloc] initBoardWithInt:x*10+y State:0];
[Boardfield.view setFrame:CGRectMake(300 +x*65-60, 30 +y*65-60, 60, 60)];
UITapGestureRecognizer* FieldTouch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ClickedPawn:)];
[Boardfield.view addGestureRecognizer:FieldTouch];
FieldTouch.numberOfTapsRequired = 1;
Boardfield.view.backgroundColor = [UIColor brownColor];
[self.view addSubview:Boardfield.view];
[Boardfield.view setTag:x*10+y];
[Board addObject:Boardfield];
}
}
}
这是Init Action:
-(id)initBoardWithInt:(int)Location State:(int)Property{
self = [super init];
if (!self) {
return nil;
}
Location = Loc;
Property = ID;
return self;
}
位置是棋盘中字段位置的属性,State是其上的图形ID(0表示无)。
现在,我希望使用Tap Gesture Recognizer或标签号访问对象。有没有办法获得循环创建的活动MainGame *对象?