实现 UISplitViewController 主从应用程序 - UIPopOverController 在旋转之前为空。

Posted

技术标签:

【中文标题】实现 UISplitViewController 主从应用程序 - UIPopOverController 在旋转之前为空。【英文标题】:Implementing a UISplitViewController Master Detail Application - UIPopOverController is null until rotation. 【发布时间】:2012-10-18 18:43:38 【问题描述】:

我正在尝试实现一个简单的 UISplitViewController,其中在纵向模式下主视图被隐藏并在弹出窗口中显示一个按钮。

我的 AppDelegate 是这样设置的:

       - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions     

            // Override point for customization after app launch.
            self.splitViewController =[[UISplitViewController alloc]init];
            self.rootViewController=[[RootViewController alloc]init];
            self.detailViewController=[[FirstDetailViewController alloc]init];

            UINavigationController *rootNav=[[UINavigationController alloc]initWithRootViewController:rootViewController];
            UINavigationController *detailNav=[[UINavigationController alloc]initWithRootViewController:detailViewController];


            self.splitViewController.viewControllers=[NSArray arrayWithObjects:rootNav,detailNav,nil];
            self.splitViewController.delegate=self.detailViewController;
            splitViewController.presentsWithGesture = NO;

            // Add the split view controller's view to the window and display.
            [self.window setRootViewController:self.splitViewController];

[window makeKeyAndVisible];

        return YES;
    

在 FirstDetailViewController 我设置了一个这样的按钮:

UIImage *buttonImage = [UIImage imageNamed:@"button-menu.png"];
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [aButton setImage:buttonImage forState:UIControlStateNormal];
    aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
    [aButton addTarget:self action:@selector(showNavigation) forControlEvents:UIControlEventTouchUpInside];
    aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
    self.appDelegate.rootPopoverButtonItem = aBarButtonItem;

showNavigation 看起来像这样:

-(void)showNavigation

    NSLog(@"I am in show navigation and pc=%@",self.popoverController);

    [self.popoverController presentPopoverFromRect:self.view.frame
                                            inView:self.view
                          permittedArrowDirections:UIPopoverArrowDirectionAny
                                          animated:YES];

当我最初加载应用程序并按下按钮时,我得到一个 NSLog

I am in show navigation and pc=(null)

并且弹出窗口不显示。现在,当我将设备旋转到横向模式,然后返回纵向时,主视图会按预期隐藏和显示,并且按钮现在可以在纵向模式下工作。

这是处理旋转的代码:

- (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc 


    [[self navigationItem] setLeftBarButtonItem:aBarButtonItem];
    [self setPopoverController:pc];
    self.appDelegate.rootPopoverButtonItem = aBarButtonItem;




// Called when the view is shown again in the split view, invalidating the button and popover controller.
- (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem 
    [[self navigationItem] setLeftBarButtonItem:nil];
    [self setPopoverController:nil];
    self.appDelegate.rootPopoverButtonItem = aBarButtonItem;


如何让 popovercontroller 在任何旋转发生之前不为空?

【问题讨论】:

【参考方案1】:

好吧,我的 willHideViewController 中有一个 if,它不包含 [self setPopoverController:pc];线。呵呵!

【讨论】:

以上是关于实现 UISplitViewController 主从应用程序 - UIPopOverController 在旋转之前为空。的主要内容,如果未能解决你的问题,请参考以下文章

UISplitViewController - 并排或纵向叠加

自定义 UISplitViewController?

SwiftUI 中的 UISplitViewController 等价物是啥

UISplitViewController 共享相同的 Header

UISplitViewController 从任何地方平移到主视图

在 UISplitViewController 中重用详细视图控制器