5

わかりました、何か重要なものを見逃しているようで、答えが見つからないようです。非常に小さいため、すべてのコードを投稿しています。

誰かが私が間違っていることを教えてもらえますか? 私はかなり長い間、例を次々と見てこれに取り組んできましたが、何もうまくいかないようです。

アプリを作成するときは、UIViewController を提供するスケルトンを使用します。コントローラーにビューを配置しました。コントローラーにリンクする変数を作成します。nib の 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 のタイプを変更してみてください (pic を参照)。

代替テキスト http://img.skitch.com/20090627-xetretcfubtcj7ujh1yc8165wj.jpg

于 2009-06-27T22:28:08.053 に答える
14

drawRectUIViewは from ではなく fromのメソッドであるUIViewControllerため、呼び出されていません。

あなたの場合、サブクラスではなく、カスタムを作成してUIView上書きする必要があるようです。drawRectUIViewController

于 2009-06-27T22:23:19.530 に答える