iPhone:为什么drawRect没有被调用?

发布于 2024-07-25 13:52:21 字数 2939 浏览 4 评论 0原文

好吧,我想我错过了一些重要的事情,而且我似乎找不到答案。 我发布了所有代码,因为它非常小。

有人可以告诉我我做错了什么吗? 我已经研究了这个例子很长一段时间了,但我所做的似乎都不起作用。

当我创建应用程序时,我使用为我提供 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱已欠费 2024-08-01 13:52:21

drawRect:是UIView子类的一个方法。 尝试子类化 UIView 并更改 InterfaceBuilder 中 UIView 的类型(见图)。

替代文本http://img.skitch.com/20090627-xetretcfubtcj7ujh1yc8165wj.jpg

drawRect: is a method for UIView subclasses. Try to subclass UIView and change the type of the UIView in InterfaceBuilder (see pic).

alt text http://img.skitch.com/20090627-xetretcfubtcj7ujh1yc8165wj.jpg

李不 2024-08-01 13:52:21

drawRect 是来自 UIView 的方法,而不是来自 UIViewController 的方法,这就是它没有被调用的原因。

在您的情况下,您似乎需要创建自定义 UIView 并覆盖其中的 drawRect ,而不是在您的 UIViewController 子类中。

drawRect is a method from UIView and not from UIViewController, that's why it's not being called.

In your case, it seems that you need to create your custom UIView and overwrite drawRect in it, and not in your UIViewController subclass.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文