iPhone:为什么不调用drawRect? [英] iPhone: why isn't drawRect getting called?

查看:123
本文介绍了iPhone:为什么不调用drawRect?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我想我错过了一些重要的东西,我似乎无法找到答案。我发布了所有代码,因为它非常小。

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.

当我创建应用程序时,我使用的是任何一个骨架给了我一个UIViewController。我把一个视图放到控制器上。我创建链接到控制器的变量。当我尝试将笔尖中的UIView连接到我的UIView时,编译器可以使用它,但它也坚持连接到视图,或者应用程序崩溃。我不会感到惊讶,如果这是问题,但如果是,我无法弄清楚如何解决它。

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.

这是代码:

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


推荐答案

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).


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

这篇关于iPhone:为什么不调用drawRect?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆