【问题标题】:AppDelegate should call the method if the screen orientation has changed如果屏幕方向发生变化,AppDelegate 应该调用该方法
【发布时间】:2013-12-05 06:11:12
【问题描述】:

我有一个popover,我从TabBarItem 呈现它,这就是我在AppDelegate 中制作它的原因,但如果屏幕方向发生变化,我需要在新位置重新显示popover。在我的应用程序的其他地方,我只使用didRotateFromInterfaceOrientation 方法,但它没有在AppDelegate 中调用。我该如何解决这个问题?

我通过这段代码展示了一个弹出框:

- (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;
}

【问题讨论】:

    标签: ios objective-c appdelegate screen-rotation


    【解决方案1】:

    您可以按如下方式注册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]);
    }
    

    享受。 :)

    【讨论】:

    • 太棒了!感谢您的及时回复!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 2023-03-16
    • 2022-07-29
    相关资源
    最近更新 更多