开发者

Adding custom UIView to UIViewController in - (void)loadView (Objective-C)

开发者 https://www.devze.com 2023-04-03 12:46 出处:网络
I am taking a programming class (for noobs) and I need to create the UIViewController graphViewController\'s view programmatically (without interface builder).

I am taking a programming class (for noobs) and I need to create the UIViewController graphViewController's view programmatically (without interface builder).

The view is simple, it only consists of an IBOUtlet which is an instance of a UIView subclass called GraphView. graphView responds to several multitouch gestures for zooming and panning and what-not, but I handle all of that stuff in - (void)viewDidLoad.

I am doing just the creation of the self.view property and graphView in the code below:

- (void)loadView
{
    UIView *gvcView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    self.view = gvcView;
    [gvcView release];

    GraphView *aGraph = [[GraphView alloc] initWithFrame:self.view.bounds];
    self.graphView = aGraph;
    [aGraph release];

}

When I run the app I do not see the graphView view in there. I just get a transparent view which shows the "My Universal App" label. I'm stumped. Please help.

Let me know if you need additional code.

Thanks!

Update: Big thanks to BJ Homer for the quick fix!

had to do the following:

add this line of code: [self.view addSubview:self.graphView]; at the end.

I was also getting this strange bug where graphView was showing up as completely black. This line of code fixed that: self.graphView.background开发者_运维技巧Color = [UIColor whiteColor];

And that's it!

Final question: Is the default background color black for a custom UIView?

Thanks again!


[self.view addSubview:self.graphView];

Until you add your graph view to a parent view, UIKit doesn't know where to display it. self.view is special, since that's the property that -loadView is supposed to set. That view will automatically be added to the screen. But your graph view is just floating off in the ether until you add it somewhere.

0

精彩评论

暂无评论...
验证码 换一张
取 消