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
The problem was the UIViewController was farkled...don't know how, but once it was replaced, it worked like a champ! Thank you all for your responses...
That really should work. Was your view controller (PreferencesViewController) properly alloc'd and init'd? Did you @synthesize your subview?
Although it shouldn't matter, you could try using floats for the CGRect (add a .0 to the end of each).
Try moving your sub view instantiation code to the method viewWillAppear of your main view. This guarantees everything has been initialized.