好吧,我想我错过了一些重要的东西,我似乎无法找到答案。我发布了所有代码,因为它非常小。
某人可以告诉我我做错了什么吗?我已经研究了这个例子,在一个例子之后,很长一段时间,我做的一切似乎都没有用。
当我创建应用程序时,我使用任何骨架给我一个UIViewController。我把一个视图放到控制器上。我创建链接到控制器的变量。当我尝试将笔尖中的UIView连接到我的UIView时,编译器可以使用它,但它也坚持连接到“视图”,或者应用程序崩溃。如果这是问题,我不会感到惊讶,但如果是,我无法弄清楚如何修复它。
以下是代码:
DotsieAppDelegate.h:
#import <UIKit/UIKit.h>
@class DotsieViewController;
@interface DotsieAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
DotsieViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet DotsieViewController *viewController;
@end
DotsieAppDelegate.m
#import "DotsieAppDelegate.h"
#import "DotsieViewController.h"
@implementation DotsieAppDelegate
@synthesize window;
@synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end
DotsieViewController.h
#import <UIKit/UIKit.h>
@interface DotsieViewController : UIViewController {
UIView *dotsieView;
}
@property (nonatomic, retain) IBOutlet UIView *dotsieView;
@end
DotsieViewController.m
#import "DotsieViewController.h"
@implementation DotsieViewController
@synthesize dotsieView;
-(void)drawRect:(CGRect)rect {
NSLog(@"here");
CGRect currentRect = CGRectMake(50,50,20,20);
UIColor *currentColor = [UIColor redColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
CGContextSetFillColorWithColor(context, currentColor.CGColor);
CGContextAddEllipseInRect(context, currentRect);
CGContextDrawPath(context, kCGPathFillStroke);
// [self.view setNeedsDisplay];
}
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
[self.view setNeedsDisplay];
return self;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
答案 0 :(得分:15)
drawRect:是UIView子类的方法。尝试继承UIView并在InterfaceBuilder中更改UIView的类型(参见图片)。
alt text http://img.skitch.com/20090627-xetretcfubtcj7ujh1yc8165wj.jpg
答案 1 :(得分:14)
drawRect
是来自UIView
而不是来自UIViewController
的方法,这就是为什么它没有被调用。
在您的情况下,您似乎需要在其中创建自定义UIView
并覆盖drawRect
,而不是在UIViewController
子类中。