2009-06-27 10 views
5

Dobra, chyba brakuje mi czegoś ważnego i nie mogę znaleźć odpowiedzi. Publikuję cały kod, ponieważ jest bardzo mały.iPhone: dlaczego drawRect nie jest wywoływany?

Czy ktoś może proszę powiedz mi, co robię źle? Pracowałem nad tym, patrząc na przykład za przykładem, przez jakiś czas, i nic, co robię, nie działa.

Kiedy tworzę aplikację, używam tego szkieletu, który daje mi UIViewController. Wrzuciłem widok na kontroler. Tworzę zmienne do połączenia z kontrolerem. Kiedy próbuję połączyć UIView w moim końcówce do mojego UIView, kompilator jest w porządku z tym, ale jednocześnie nalega, aby być połączonym z "widokiem", albo aplikacja się zawiesza. Nie zdziwiłbym się, gdyby to był problem, ale jeśli tak, nie mogę wymyślić, jak to naprawić.

Oto kod:

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 

Odpowiedz

14

drawRect jest metodą z UIView a nie od UIViewController, dlatego nie jest to miano.

W twoim przypadku wydaje się, że musisz utworzyć niestandardowy UIView i zastąpić w nim drawRect, a nie w podklasie UIViewController.

Powiązane problemy