首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iPhone:为什么不给drawRect打电话?

iPhone:为什么不给drawRect打电话?
EN

Stack Overflow用户
提问于 2009-06-27 22:18:58
回答 2查看 14.4K关注 0票数 5

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

有人能告诉我我做错了什么吗?我在这个例子上工作了很长一段时间了,但我做的一切似乎都不起作用。

当我创建这个应用程序时,我使用任何一个骨架给我一个UIViewController。我在控制器上放了一个视图。我创建要链接到控制器的变量。当我尝试将我的UIView连接到我的UIView时,编译器是可以接受的,但它也坚持要连接到“视图”,否则应用程序就会崩溃。如果这是问题的话,我不会感到惊讶,但如果是的话,我不知道如何解决它。

下面是代码:

DotsieAppDelegate.h:

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
#import <UIKit/UIKit.h>

@interface DotsieViewController : UIViewController {

    UIView *dotsieView;

}

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

@end

DotsieViewController.m

代码语言:javascript
运行
复制
#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
EN

回答 2

Stack Overflow用户

发布于 2009-06-27 22:28:08

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

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

票数 15
EN

Stack Overflow用户

发布于 2009-06-27 22:23:19

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

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

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1053761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档