views:

23

answers:

1

My app delegate contains:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  // Override point for customization after app launch    
  window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  if (!window) 
  {
    [self release];
    return;
  }

  window.backgroundColor = [UIColor greenColor];
  [window addSubview:viewController.view];
  [window makeKeyAndVisible];
  [window layoutSubviews];
}

This does get executed, apparently with no errors. The window variable is NOT 0 so the test if (! window) does not cause the function to return. However no green-background window appears, just the default color. And in the appController.m file, code in the viewDidLoad method does execute. However CGContextRef context = UIGraphicsGetCurrentContext(); returns null rather than a real context.

What am I leaving out?

A: 

I suspect your window is not green because the view you're adding with [window addSubview:viewController.view]; is opaque and covering the entire window.

As to the other problem, if you're asking for the current graphics context in viewDidLoad, I don't think there is guaranteed to be one. You can only draw on UIViews by subclassing and overriding their drawRect:(CGRect) member. If you want to draw outside these, you'll need to create your own graphics context via something like CGBitmapContextCreate, then display the results either by drawing them in a view's drawRect or by finding another control that will take a CGImage you've made with the bitmap functions (i.e. UIImage).

Adam Wright
Thanks. I commented out the addSubview and then the screen still turns up with the default color BUT when I stop the simulator, my background color shows just before it all goes off - I changed the color to verify this. So you are apparently correct but something else is also happening.
Jonathan Starr
If I have to subclass a UIView to override its drawRect, wouldn't that mean that any UIView directly instantiated would be useless, because it would not have an override drawRect? Probably I am not getting something - how could a view created in Interface Builder be drawn on?
Jonathan Starr
Indeed, UIViews that are not subclassed have little to no behaviour - they just draw white squares. You can set UIViews created with Interface Builder to be an instance of whatever UIView subclass you want using the "Class" section of the Identity inspector.
Adam Wright