如果屏幕方向已更改,AppDelegate应调用该方法

时间:2013-12-05 06:11:13

标签: ios objective-c appdelegate screen-rotation

我有一个popover,我在TabBarItem中提到了这就是为什么我在AppDelegate中提出这个问题,但是我需要在新的位置重新显示popover方向已经改变。在我的应用的其他地方,我只使用didRotateFromInterfaceOrientation方法,但它没有在AppDelegate中调用。我该如何解决这个问题?

我通过以下代码提出了一个popover:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        if (viewController == [self.objTabBarController.viewControllers objectAtIndex:2])
        {
            if (self.settingsPopover == nil) {
                UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main - iPad" bundle: nil];
                SettingsViewController *settingsController = [mainStoryboard instantiateViewControllerWithIdentifier: @"SettingsPopover"];

                UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:settingsController];

                self.settingsPopover = [[UIPopoverController alloc] initWithContentViewController:navigationController];

                CGSize tabBarSize = self.objTabBarController.tabBar.frame.size;
                CGPoint center = [self.window.rootViewController.view convertPoint:self.objTabBarController.tabBar.center fromView:self.objTabBarController.tabBar.superview];
                center.x += 105;
                center.y -= tabBarSize.height / 2;
                CGRect rect = {center, CGSizeZero};

                [self.settingsPopover presentPopoverFromRect:rect inView:self.window.rootViewController.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
            } else {
                [self.settingsPopover dismissPopoverAnimated:NO];
                self.settingsPopover = nil;
            }

            return NO;
        }
    }
    return YES;
}

1 个答案:

答案 0 :(得分:5)

您可以注册Notification,如下所示:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleDidChangeStatusBarOrientationNotification:) 
                                             name:UIApplicationDidChangeStatusBarOrientationNotification 
                                           object:nil];

然后,在AppDelegate

中实施它
- (void)handleDidChangeStatusBarOrientationNotification:(NSNotification *)notification;
{
  NSLog(@"The orientation changed to %@", [notification.userInfo objectForKey: UIApplicationStatusBarOrientationUserInfoKey]);
}

享受。 :)