0
votes

I have added a view inside ViewController using [self.view addSubview:newView]. newView is a UIView type object. Inside newView I added added a few views using [self addSubview:polyg];. Polygs also inherit from UIView. But they never get drawn. drawRect:(CGRect)rect is never called.

I'm new to iOS and have no idea what I'm doing wrong.

It probably has something to do with the fact that I did not use self.view when adding views to newView. I can't even use self.view inside newView.

how can I make this work?

edit: When i create these polyg objects I'm using initWithFrame and sending newView's frame, like this: [[Polyg alloc] initWithFrame:self.frame]; Could this be the cause? Polyg is going to be a movable and resizable polygon, so I figured each should be able to draw on the entire screen.

edit: I created a method inside newView called touchX:Y that is called from the viewController whenever I touch my finger on the screen. Inside I wrote this: NSLog(@"subview = %i", self.subviews.count);. Whenever I touch the screen I see this on the console: subview = 89 so I'm pretty sure the subviews were added to newView.

I tried adding this to the same touchX:Y method:

        for (UIView* subview in self.subviews) {
            [subview setNeedsDisplay];
        }

But the subviews' drawRect is never called.

1
You can add nested subviews as deep as you need. Just remember that a view's frame is always relative to its immediate superview (parent view).rmaddy
Yes, you can add subviews to subviews. If you really called your method DrawRect, it won't get called. The name of the method must be drawRect:. It must be correctly capitalized and must take one CGRect argument. If you did call it drawRect:, how do you know it's not being called?rob mayoff
- (void)drawRect:(CGRect)rect { // Drawing code NSLog(@"drawRect!"); } drawRect never shows up on the console. (edited the question with the correct method name)HSNN
If newView is a view, it hasn't a property called view. View controllers have it.Ramy Al Zuhouri
It is a view and I can't use self.view inside it. Is that the problem? I can add subViews to the viewController but can't add a subView to a subView?HSNN

1 Answers

1
votes

The most likely possibility is that newView is either completely off the screen or have a size of (0,0). Right before you add the ployg subviews, use the debugger to print out the frame of newView.