سؤال

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.

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top