0

デバイスの向きをチェックするアプリケーションを作成しています。そのため、次のコードブロックがあります。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(orientationChanged:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:[UIDevice currentDevice]];

次に、次のメソッドを呼び出します。

- (void) orientationChanged:(NSNotification *)note {
    ...
}

私がやりたいのは、別のセクションから投稿した最初のコードブロックとは別に、上記のメソッドを呼び出すことです。これは可能ですか?もしそうなら、どのように?

4

3 に答える 3

2

このタイプの状況で私が通常行うことはnil、引数として渡すことです。

[self orientationChanged:nil];

これは、通知自体がメソッドの実装にとってどれほど重要であるかによって異なります。適切な情報を含む通知を作成する必要がある場合があります。

NSNotification *n = [NSNotification notificationWithName:@"someName" object:someObject];
[self orientationChanged:n];

しかし、私はこのタイプのニーズをコードの臭いと見なすようになりました。代わりに、通知ハンドラーが実行する作業を別のメソッドに抽出し、それを直接呼び出すことを試みます。例:

- (void)handleOrientationChangeForDevice:(UIDevice *)d {
    // do something here
}

- (void)orientationChanged:(NSNotification *)n {
    [self handleOrientationChangeForDevice:n.object];
}

次に、呼び出し元のコードで、次のようなことを行うことができます。

[self handleOrientationChangeForDevice:[UIDevice currentDevice]];
于 2013-01-05T05:33:49.700 に答える
0

通知を待たずにデバイスの向きが必要な場合は、次の方法で取得できます。

UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
于 2013-01-05T05:17:31.953 に答える
0

nilパラメータまたはNSNotificationタイプのオブジェクトを渡すことで呼び出すことができます。

同じクラスの.mで呼び出すには-

[self orientationChanged:nil];

別のクラスから呼び出すには-

[controller orientationChanged:nil]; //Declare method in .h first
于 2013-01-05T05:33:26.617 に答える