Right now I have a UIView
with two subviews on it. I am trying to add another subview to one of those views, from a custom-made Card
class. The problem is that the subview is coming from an NSMutableArray, and it makes the program crash every time I try to add it to any views. I know the issue has something to do with the NSMutableArray, because there isn't any problem if I just try to add an instance of Card that is initialized right then and there. What's the problem?
Here's the code:
Initialization of the main view where this is happening:
- (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;
}
method where the card is supposed to be added:
- (void)viewDidLoad
{
[super viewDidLoad];
Card* card = [deck objectAtIndex:0];
[hand addSubview:card];
[self.view addSubview:hand];
[self.view addSubview:board];
[self.view addSubview:mainMenu];
}
and here is where the getDeck
method is, in the Reader
class:
-(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;
}