2012-10-18 44 views
2

我正試圖實現一個簡單的UISplitViewController,其中在縱向模式下,主視圖是隱藏的,並在彈出窗口中顯示一個按鈕。實現UISplitViewController主細節應用程序 - 直到旋轉,UIPopOverController爲空。

我的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不爲空?

回答

0

事實證明,我有一個如果在我的willHideViewController沒有包括[self setPopoverController:pc];線。咄!

相關問題