在UIView上绘制一个Rect

时间:2014-11-24 18:07:47

标签: ios objective-c uiview draw rect

我点击按钮时试图在UIView上绘制一个矩形,但由于某种原因,它还没有工作......

- (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];
}

警报工作正常,但直接没有。

1 个答案:

答案 0 :(得分:0)

正如@matt在评论中提示的那样,最常见的原因是你没有正确地将你的属性或字段绑定到你的UI组件,否则你已经尝试在加载UIView之前运行这段代码。在任何一种情况下,_areaGame都是nil,addSubview调用无声地执行任何操作。在addSubview行上设置断点并检查_areaGame是最快的验证方式。