5

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

有人可以告诉我我做错了什么吗?很长一段时间以来,我一直在研究这个一个又一个的例子,但我所做的似乎没有任何效果。

当我创建应用程序时,我使用任何一个框架给我一个 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
4

2 回答 2

15

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

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

于 2009-06-27T22:28:08.053 回答
14

drawRect是 fromUIView而不是 from的方法UIViewController,这就是它不被调用的原因。

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

于 2009-06-27T22:23:19.530 回答