I'm only starting to developing iOS apps. So the question is gonna be nooby i guess..
I'm trying to understand how the mvc model works in iOS developing.
I want to mix available views and my own (which I want to draw by myself using primitives such as drawing strings)
So
I created the MyView:UIView class. Then I overrided dr开发者_JAVA技巧awRect method and added there a simple output. Then I added my class to the "ipad display" using interface builder.
And the problem is that breakpoint in the beginning of the DrawRect method never gets reached. And of course I get a blank screen in simulator=(
I've tried to add ViewController and use it in the interface builder but in fact i don't need it because my view just draws a constant string and that's all...
I guess I misunderstand something very serious because looking through apple examples got nothing for me.
Here's my code (sorry, forgot about it):
MyView.h:
@interface MyView : UIView {
...
}
MyView.m:
...
- (void)drawRect:(CGRect)rect {
NSString *text = @"Hello, world";
UIFont *font = [UIFont systemFontOfSize:10];
[[UIColor blackColor] set];
CGPoint point;
point = CGPointMake(0.5f, 0.5f);
[text drawAtPoint:point withFont:font];
[font release];
}
UPD:
I've added a TabBar just to make sure that the problem is only with my own view. That's it: TabBar appears.
As you didn't post your code, this is a wild guess, but you need to make the view re-render:
[myViewInstance setNeedsDisplay:YES];
Also, make sure the view is actually loaded. You can check this with a little NSLog in the nib awaking method (-[<NSNibAwaking> awakeFromNib]
).
If drawRect:
is not being called, then either you are not actually onscreen, or you have put in the wrong signature for drawRect:
. The correct signature is:
- (void)drawRect:(CGRect)rect
To make sure you're actually onscreen, try overloading awakeFromNib
and putting a breakpoint there. If you're not, then you have not configured it correctly in interface builder. The most common problems are failure to set the correct class for the view (it should be set to MyView), or putting it into a NIB that is not actually loaded.
And of course make sure that you are actually a subclass of UIView
, that you do not have any warnings from the compiler or from Interface Builder, and that there are no messages printed to console when you run.
精彩评论