好吧,我想我错过了一些重要的事情,而且我似乎找不到答案。我把所有的代码都发了出来,因为它很小。
有人能告诉我我做错了什么吗?我在这个例子上工作了很长一段时间了,但我做的一切似乎都不起作用。
当我创建这个应用程序时,我使用任何一个骨架给我一个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;
@endDotsieAppDelegate.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];
}
@endDotsieViewController.h
#import <UIKit/UIKit.h>
@interface DotsieViewController : UIViewController {
UIView *dotsieView;
}
@property (nonatomic, retain) IBOutlet UIView *dotsieView;
@endDotsieViewController.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发布于 2009-06-27 22:28:08
drawRect:是UIView子类的一种方法。尝试将UIView子类化,并在InterfaceBuilder中更改UIView的类型(参见图)。
alt text http://img.skitch.com/20090627-xetretcfubtcj7ujh1yc8165wj.jpg
发布于 2009-06-27 22:23:19
drawRect是来自UIView的方法,而不是来自UIViewController的方法,这就是为什么没有调用它的原因。
在您的示例中,您似乎需要创建自定义UIView并在其中覆盖drawRect,而不是在UIViewController子类中。
https://stackoverflow.com/questions/1053761
复制相似问题