2

縦向きと横向きの両方で動作する必要があるアプリがあり、UITabBar は現在の向きに調整する必要があります (カスタムの背景と選択されたアイテムがあります)。したがって、残りのビューでは- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientationメソッドをオーバーライドするだけで、完全に機能します。

の に対してどうすればよい.moreNavigationControllerですUITabBarControllerか?オブザーバーを追加しようとしました(セレクターは UITabBarController の拡張にあります):

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:self.moreNavigationController];

しかし、それは決して呼び出されません。何か不足していますか、またはこの状況を処理する最善の方法は何ですか?

解決策: なぜかUIDeviceOrientation正しく発火しないので、使用したほうがよいstatusBarOrientation, チャームとして機能します.

機能する最終的なコードは次UITabBarControllerviewDidLoadとおりです。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(didRotate:)
                                             name:UIApplicationDidChangeStatusBarOrientationNotification
                                           object:nil];

セレクターdidRotateメソッド:

- (void) didRotate:(NSNotification *)notification{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if(UIInterfaceOrientationIsPortrait(orientation)) {
        // Portrait
    } else {
        // Landscape
    }
}

手伝ってくれてありがとう。

4

3 に答える 3

2

UITabBarController投稿されない通知を登録しています。メソッドのドキュメントNSNotificationCenter Referenceを見てください。addObserver:selector:name:object

- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender

notificationSender: オブザーバーが受け取りたい通知のオブジェクト。つまり、この送信者によって送信された通知のみがオブザーバーに配信されます。nil を渡すと、通知センターは通知の送信者を使用して通知をオブザーバーに配信するかどうかを決定しません。

そのため、送信者として を指定する. moreNavigationControllerと、そのような通知は投稿されないため、それらの通知を受け取ることはありません。代わりnilに、送信者を無視して、送信者に関係なくステータス バーの変化をリッスンするように渡します。

ちなみに、このSO Answerでは、向きの変化にどのように対応できるかをまとめています。

そしてついに。それでもうまくいかない場合は、次の方法を試してください。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(didRotate:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil]; 
于 2012-11-06T17:54:07.253 に答える
1

Yes, you forgot to post notification, which will call you own notification:

[[NSNotificationCenter defaultCenter] postNotificationName: UIApplicationDidChangeStatusBarOrientationNotification object:self];

or if you dont wont to send anything just set object as nil:

[[NSNotificationCenter defaultCenter] postNotificationName: UIApplicationDidChangeStatusBarOrientationNotification object:nil];
于 2012-11-04T18:52:54.613 に答える
0

同じものを実装する最善の方法は、最初に addObserver を追加し、次にオブザーバーを削除してクラッシュを回避することです:-

-(void)viewDidLoad{
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didRotate:)
                                                 name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}

//Now Remove Observer
-(void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self @"UIDeviceOrientationDidChangeNotification" object:nil];
}
于 2015-09-19T04:23:55.407 に答える