2

私はiPadプロジェクトを次のように構成しています:-AppDelegate-MainWindow-ViewController-View

View Controllers .mファイルは、プログラムで別のビューをロードし、それを画面に配置します。このビューはスライドインおよびスライドアウトされます。

私はこれをします:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect  viewRect = CGRectMake(0, 0, 0, 0);
    CalculatorView *v = [[[CalculatorView alloc] 
                                    initWithFrame:viewRect] autorelease];
    [self.view.window addSubview:v];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    v.view.frame   = CGRectMake(0, 0, 460, 320);
    [UIView commitAnimations];
}

私が抱えている問題は、ここで追加しているサブビューの方向が正しくないように見えることです。プロジェクトはランドスケープのみをサポートし、ランドスケープを起動します。コンテナビューは問題なく、ボタンもいくつか含まれています。ただし、このプログラムでロードされたビューはポートレートモードでスタックします。次の自動回転コードを提供しました(ロードされたビューの.m内):

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

しかし、それは決して呼び出されません。

では、プログラムで追加されたサブビューをポートレートモードではなくランドスケープモードでロードするにはどうすればよいですか?TIA!

4

1 に答える 1

1

UIViewクラスは、方向変更メッセージを受信しません。

(特に、shouldAutorotateToInterfaceOrientationUIViewControllerメソッドであるメソッド)

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW23

ビューにメソッドを手動で追加して、向きが変更されたことを通知する必要があります。このメソッドは、コントローラーshouldAutorotateToInterfaceOrientationメソッドで呼び出す必要があります。

これを行うには、コントローラーで表示する参照を作成し、自分でメモリを管理する必要があります。

@interface MyController : UIViewController {

   CalculatorView *_calculatorView;

}

@end

@implementation MyController


- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect  viewRect = CGRectMake(0, 0, 0, 0);

   //inits _calculatorView without the autorelease. Will be released in the dealloc method
    _calculatorView = [[CalculatorView alloc] 
                                    initWithFrame:viewRect];
    [self.view.window addSubview:v];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    _calculatorView.view.frame   = CGRectMake(0, 0, 460, 320);
    [UIView commitAnimations];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
   //calls custom interface orientation method
   [_calculatorView MyInterfaceChangedCustomMethod:interfaceOrientation];

    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

-(void) dealloc { 

   [_calculatorView release];
   [super dealloc];
}



@end

編集:CalculatorViewが単純で、デバイスの回転後にフレームを適切に変更するだけでよい場合は、次のようなビューのautoresizingMaskを使用するのが最善の方法だと思います。

_calculatorView = [[CalculatorView alloc] 
                                    initWithFrame:viewRect];
_calculatorView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
于 2011-05-11T01:29:56.750 に答える