Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

printf

macrumors regular
Original poster
Aug 27, 2008
105
0
i've created a custom view controller and a custom view programmatically and while everything compiles fine, i only see a white screen in the iphone simulator *except* when i press the home button, where i can THEN see the drawing i've done to the view as it shrinks into obscurity. any idea why i'm not seeing my view while the app is actively running?

here's the relevant code:

app delegate's didFinishLaunchingWithOptions
Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
	CGRect rect = CGRectMake(0.0, 0.0, 320, 480);
	window = [[UIWindow alloc] initWithFrame:rect];		
	
	viewController = [[ViewController alloc] init];	
	
	[window addSubview:viewController.view];
        [window makeKeyAndVisible];	
        return YES;
}

view controller's loadView:
Code:
- (void)loadView {
	CGRect cgRct = CGRectMake(0.0, 0.0, 320, 480);
	myView = [[CustomView alloc] initWithFrame:cgRct];
	myView.autoresizesSubviews = YES;
	self.view = myView; 
}

custom view's drawRect:
Code:
- (void)drawRect:(CGRect)rect {
	CGContextRef theContext = UIGraphicsGetCurrentContext();
	
	CGContextSetRGBFillColor(theContext, .5, .5, .5, 1);
	CGContextFillRect(theContext, rect);	
	
	NSLog(@"drawRect"); //yes, it get's called
}

again, i do see the rectangle drawn above, but only AFTER the home button has been hit to close the app.

ps. this really doesn't need justification, but this IB avoidance is a one-off thing, so just bear with me and thanks in advance for your help :)
 
Weird result

Well, given the code above, I didn't managed to mimic the result you got though, I suppose I have the result that you might be interested.

OK, the code you stated seem quite fine, have you checked that you've declared that view controller a IBOutlet and added it in your MainWindow.xib? If you didn't, then it won't work. And, make sure that this view controller doesn't come with a default view, because you're gonna implement a customized view yourself. Beside that, I suppose you should get what you're looking for. So, good luck.
 
thanks akira, but that's the point. i'm *not* using Interface Builder for this. I'm creating the window, view controller, and custom view, all programmatically for this project. As such, I want to wire them up programmatically as well :)

anyone idea of what i'm doing wrong? i can show more code, but what i've already offered accounts for most of the relevant parts i think.
 
How about...

thanks akira, but that's the point. i'm *not* using Interface Builder for this. I'm creating the window, view controller, and custom view, all programmatically for this project. As such, I want to wire them up programmatically as well :)

anyone idea of what i'm doing wrong? i can show more code, but what i've already offered accounts for most of the relevant parts i think.

Well, I tried again not to use Interface builder this time and get the correct result as you might need. Strangely, the code I wrote is almost identical to yours given, yet I couldn't find why you didn't get the result you wanted until you hit the home button. Perhaps you might wanna have a thorough comparison of my project file against yours, so, I'm attaching a copy of my xcode project file and you can have a look. Please keep me notified if you find where the problem lies.
 

Attachments

  • test.zip
    19.3 KB · Views: 293
I assume you are deriving from UIView. If it didn't invoke drawRect: automatically, maybe you can force it call to setNeedsDisplay after assigning the view to the controller. (I'm just guessing though!)
 
akira, thank you, i greatly appreciate your help :)
here was the problem:

Code:
CGRect rect = CGRectMake(0.0, 0.0, 320, 480);
window = [[UIWindow alloc] initWithFrame:rect];

i'm guessing since i still had my MainWindow_Phone.xib which contained a Window called window, trying to allocate that caused the problem. removing that line above allowed it to work.

thanks again!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.