2

This is my code where I'm trying to create a subView... I'm using XCode4 with StoryBoards. The app is crashing on the 2nd line where it is allocating subView with EXC_BAD_ACCESS. vFrame has valid content. What is wrong with this? (I'm using XCode4 with Storyboards, btw).

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect vFrame = CGRectMake(60,100,200,200);
    subView = [[UIView alloc] initWithFrame:vFrame];
    subView.backgroundColor = [UIColor redColor];
    [self.view addSubview: subView];
}

UPDATE: definition for subView:

@interface PreferencesViewController : UIViewController  {

    UIView *subView;
}

@property (nonatomic, retain)  UIView *subView;

@end
4

3 回答 3

1

尝试将您的子视图实例化代码移动到您的主视图的方法 viewWillAppear 中。这保证一切都已初始化。

于 2012-04-04T21:05:04.277 回答
1

这真的应该工作。您的视图控制器 (PreferencesViewController) 是否正确分配和初始化?你@synthesize 你的子视图了吗?

虽然没关系,但您可以尝试对 CGRect 使用浮点数(在每个末尾添加 .0)。

于 2012-04-04T19:40:13.990 回答
0

问题是 UIViewController 被 farkled...不知道如何,但是一旦它被替换,它就像一个冠军!谢谢大家的回复...

于 2012-04-04T22:14:25.070 回答