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

时间:2012-10-18 18:43:39

标签: uisplitviewcontroller uipopovercontroller

我正在尝试实现一个简单的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不为null?

1 个答案:

答案 0 :(得分:0)

好的结果我在我的willHideViewController中有一个不包含[self setPopoverController:pc]的if;线。杜!