iPhone:为什么drawRect没有被调用?
好吧,我想我错过了一些重要的事情,而且我似乎找不到答案。 我发布了所有代码,因为它非常小。
有人可以请告诉我我做错了什么吗? 我已经研究了这个例子很长一段时间了,但我所做的似乎都不起作用。
当我创建应用程序时,我使用为我提供 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
Okay, I guess I'm missing something important, and I can't seem to find the answer. I'm posting all the code, because it's very small.
Can someone please tell me what I'm doing wrong? I've worked on this looking at example after example, for quite a while, and nothing I do seems to work.
When I create the app, I use whichever skeleton gives me a UIViewController. I put a view onto the controller. I create the variables to link into the controller. When I try to connect the UIView in my nib to my UIView, the compiler's okay with it, but it also insists on being connect to "view" as well, or the app crashes. I wouldn't be surprised if that's the issue, but if it is, I can't figure out how to fix it.
Here's the code:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
drawRect:是UIView子类的一个方法。 尝试子类化 UIView 并更改 InterfaceBuilder 中 UIView 的类型(见图)。
drawRect: is a method for UIView subclasses. Try to subclass UIView and change the type of the UIView in InterfaceBuilder (see pic).
drawRect
是来自UIView
的方法,而不是来自UIViewController
的方法,这就是它没有被调用的原因。在您的情况下,您似乎需要创建自定义
UIView
并覆盖其中的drawRect
,而不是在您的UIViewController
子类中。drawRect
is a method fromUIView
and not fromUIViewController
, that's why it's not being called.In your case, it seems that you need to create your custom
UIView
and overwritedrawRect
in it, and not in yourUIViewController
subclass.