我从 XCODE 4.5.2 中选择了单视图应用程序模板。我有一个 appdelegate ,一个带有 RDViewController.xib 的视图控制器,我已经制作了我的 UIView 类 Graphics 的类型。我也有 Graphics.h 和 Graphics.m(其中的 drawRect 方法)。viewController.m 中有一个 Graphics 类类型的 graphics 属性。运行时调用 drawRect 方法,但在模拟器上没有绘制任何内容。未调用 Graphics 类中的 initWithFrame 方法。RDViewController.xib 是 Graphics 的子类。
在 appdelegate 中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[RDViewController alloc] initWithNibName:@"RDViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
我只在一个 ViewController 中写过:
@implementation RDViewController
@synthesize graphics;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.graphics setNeedsDisplay];
}
在 UIView 子类 Graphics
@implementation GraphicsView
- (id)initWithFrame:(CGRect)frame
{
NSLog(@"Frame: %@", NSStringFromCGRect(frame));
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
UIFont *helveticaBold;
[UIFont fontWithName:@"HelveticaNeue-Bold" size:40.0f];
NSString *myString = @"Test string function";
[myString drawInRect:CGRectMake(20.0f,20.0f,40.0f,160.0f)
withFont:helveticaBold];
self.backgroundColor = [UIColor whiteColor];
}
运行时调用 drawRect 方法,但在模拟器上没有绘制任何内容。未调用 Graphics 类中的 initWithFrame 方法。RDViewController.xib 是 Graphics 的子类。