现在我有一个UIView
有两个子视图。我正在尝试从定制Card
类向其中一个视图添加另一个子视图。问题是子视图来自 NSMutableArray,每次我尝试将它添加到任何视图时都会使程序崩溃。我知道这个问题与 NSMutableArray 有关,因为如果我只是尝试添加一个在当时和那里初始化的 Card 实例,就没有任何问题。有什么问题?
这是代码:
初始化发生这种情况的主视图:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
mainMenu = [UIButton buttonWithType:UIButtonTypeRoundedRect];
mainMenu.frame = CGRectMake(10, 944, 100, 50);
[mainMenu setTitle:@"Main Menu" forState:UIControlStateNormal];
[mainMenu addTarget:self action:@selector(goMainMenu) forControlEvents:UIControlEventTouchUpInside];
reader = [[Reader alloc] init];
deck = [reader getDeck];
hand = [[Hand alloc] initWithFrame:CGRectMake(0, 1024/3, 768, 2048/3)];
board = [[Board alloc] initWithFrame:CGRectMake(0, 0, 768, 1024/3)];
}
return self;
}
应该添加卡片的方法:
- (void)viewDidLoad
{
[super viewDidLoad];
Card* card = [deck objectAtIndex:0];
[hand addSubview:card];
[self.view addSubview:hand];
[self.view addSubview:board];
[self.view addSubview:mainMenu];
}
这是类中getDeck
方法所在的位置Reader
:
-(NSMutableArray*)getDeck {
NSMutableArray* deck = [NSMutableArray arrayWithCapacity:72];
Card* card;
NSMutableArray* strings = [[scanner string] componentsSeparatedByString:@"\n"];
while ([strings count] > 0) {
[strings removeObjectAtIndex:0];
card = [Card cardWithExpressions:[strings subarrayWithRange:NSMakeRange(0, 4)]];
card.frame = CGRectMake(0, 0, 180, 252);
[card setCoefficient:[self getCoefficientFromExpression:[card getPolynomial]]];
[deck addObject:card];
[strings removeObjectsInRange:NSMakeRange(0, 4)];
}
return deck;
}