2011-06-15 17 views
2

UIViewから継承するGraphicViewクラスがあります。そのinitWithFrame方法は次のとおりです。ビューがプロパティの場合、InitWithFrameは実行されません。

にInterface Builderを使用して
@implementation GraphicsView 

- (id)initWithFrame:(CGRect)frameRect 
{ 
    self = [super initWithFrame:frameRect]; 

    // Create a ball 2D object in the upper left corner of the screen 
    // heading down and right 
    ball = [[Object2D alloc] init]; 
    ball.position = [[Point2D alloc] initWithX:0.0 Y:0.0]; 
    ball.vector = [[Vector2D alloc] initWithX:5.0 Y:4.0]; 

    // Start a timer that will call the tick method of this class 
    // 30 times per second 
    timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/30.0) 
              target:self 
              selector:@selector(tick) 
              userInfo:nil 
              repeats:YES]; 

    return self; 
} 

私はViewController.xibへのUIView(クラス=グラフィック・ビュー)を追加しました。そして、私はプロパティとしてGraphicViewを追加しました:

@interface VoiceTest01ViewController : UIViewController { 

    IBOutlet GraphicsView *graphView; 
} 

@property (nonatomic, retain) IBOutlet GraphicsView *graphView; 

- (IBAction)btnStartClicked:(id)sender; 
- (IBAction)btnDrawTriangleClicked:(id)sender; 

@end 

しかし、このコードでは動作しません、私はそれが動作するために[graphView initWithFrame:graphView.frame]を呼び出す必要があります。

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    isListening = NO; 
    aleatoryValue = 10.0f; 

    // Esto es necesario para inicializar la vista 
    [graphView initWithFrame:graphView.frame]; 

} 

いいですか?それを行う良い方法はありますか?

GraphicViewをプロパティとして追加すると、なぜinitWitFrameが呼び出されないのか分かりません。

答えて

3

initWithFrame NIBからロードするときは呼び出されませんが、代わりにinitWithCoderです。

あなたはNIBとプログラム作成から両方の負荷を使用している可能性がある場合は、あなたがinitWithFrameinitWithCoderの両方から呼び出すと一般的な方法(initCommonを多分?)にする必要があります。


ああ、あなたのinitメソッドは、推奨事項を使用していません。

- (id)initWithFrame:(CGRect)frameRect 
{ 
    if (!(self = [super initWithFrame:frameRect])) 
     return nil; 

    // ... 
} 

あなたは常に[super init...]の戻り値をチェックする必要があります。

+0

ありがとうございます。 initWithCoderをどのようにオーバーライドできますか?私はインターネットで検索していますが、例は見つかりませんでした。再度、感謝します。 – VansFannel

+0

' - (id)initWithCoder:(NSCoder *)coder' ViewController .mファイルのXcodeで入力を開始すると、自動的に自動補完されます。 – jv42

+1

ありがとうございます。私はまた、次のようなものを追加する必要があると思います: 'if((self = [super initWithCoder:coder])){' – VansFannel

関連する問題