Drawing a Rect on UIView

102 Views Asked by At

I'm trying to draw a rect on a UIView when a button is clicked, but for some reason isn't working...

- (IBAction)createNewGame:(id)sender {


[self dismissViewControllerAnimated:NO completion:nil];

//create the new game and add to the array of games, save the index of actual game.
FIALGameModel *newGame = [[FIALGameModel alloc] initWithRows:_row columns:_column];
[_games addObject:(newGame)];
_actualGame = [_games count]-1;
if(_debug==true) {
    NSString* messageString = [NSString stringWithFormat: @"Creating a New Game with %d rows and  %d columns.", _row, _column];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                    message: messageString
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

UIView *test = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 60, 60)];
test.backgroundColor = [UIColor redColor];
[_areaGame addSubview:test];
}

The alert works fine, but the rect don't.

1

There are 1 best solutions below

0
On

As @matt hints in comment, the most usual cause of this is that you've not bound your properties or fields to your UI components properly, or else you've tried to run this code before the UIView was loaded. In either case, _areaGame is nil and the addSubview call silently does nothing. Setting a breakpoint on the addSubview line and inspecting _areaGame is the quickest way to verify.