2012-01-08 73 views
4

我有一個UISplitViewController這是一個UISplitViewControllerDelegate具有以下的委託方法:UISplitViewController:如何呈現popover?

splitViewController:willHideViewController:withBarButtonItem:forPopoverController:

當iPad在肖像開始,我想酥料餅從SPLITVIEW可見。我怎樣才能做到這一點?

我試過下面的代碼:

- (void)splitViewController:(UISplitViewController *)svc 
    willHideViewController:(UIViewController *)aViewController 
      withBarButtonItem:(UIBarButtonItem *)barButtonItem 
     forPopoverController:(UIPopoverController *)pc 
{ 
    //setting the barButtonItem in the toolbar in the detail view. 

    [pc presentPopoverFromBarButtonItem:barButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO]; 
} 

但上面的代碼給我下面的錯誤:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.'

回答

4

那裏只有一個問題,錯誤的地點調用presentPopover方法,splitViewController: * WillHide * ViewController .......所以,barButtonItem存在但不在屏幕上。我使用下一個代碼,它爲我工作。 爲了處理所有情況,你需要使用2種方法。

- (void)viewDidAppear:(BOOL)animated 
{ 
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) { 
     if (self.view.window != nil) { 
      [_masterPopoverController presentPopoverFromRect:CGRectMake(0, 0, 1, 1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO]; 
     } 
    } 
    [super viewDidAppear:animated]; 
} 

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    if (fromInterfaceOrientation == UIDeviceOrientationLandscapeLeft || fromInterfaceOrientation == UIDeviceOrientationLandscapeRight) { 
     if (self.view.window != nil) { 
      [_masterPopoverController presentPopoverFromRect:CGRectMake(0, 0, 1, 1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO]; 
     } 
    } 
} 
+0

凡在你的代碼,你把這個代碼?我嘗試過處理PopoverController指向我的ViewController的指針,但它似乎是當我想呈現popover時,將其更改爲nil。 – dhrm 2012-01-08 21:32:40

+0

用工作和測試代碼更新了我的答案 – Darktau 2012-01-09 06:48:44

+0

謝謝你,工作! – dhrm 2012-01-09 08:02:15

相關問題