iPhone: почему не вызывается 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

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

person Community    schedule 27.06.2009    source источник


Ответы (2)


drawRect: это метод для подклассов UIView. Попробуйте создать подкласс UIView и изменить тип UIView в InterfaceBuilder (см. Рис.).

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

person epatel    schedule 27.06.2009

drawRect - это метод от UIView, а не от UIViewController, поэтому он не вызывается.

В вашем случае кажется, что вам нужно создать свой собственный UIView и перезаписать drawRect в нем, а не в вашем подклассе UIViewController.

person pgb    schedule 27.06.2009