iPhone:为什么不调用drawRect?

5 iphone quartz-graphics 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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

DotsieViewController.h

#import <UIKit/UIKit.h>

@interface DotsieViewController : UIViewController {

    UIView *dotsieView;

}

@property (nonatomic, retain) IBOutlet UIView *dotsieView;

@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

epa*_*tel 15

drawRect:是UIView子类的方法.尝试继承UIView并在InterfaceBuilder中更改UIView的类型(参见图片).

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


pgb*_*pgb 14

drawRect是一种来自UIView而不是来自的方法UIViewController,这就是为什么它没有被调用.

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