2012-04-25 109 views
6

我想玩UIPopupController,並且我明白我無法檢測到我的popover關閉時。 我的步驟:
1.創建例如在Xcode(文件 - >新建項目 - > Utilitiy應用)
2.添加到MainViewController.h UIPopoverControllerDelegate檢測popover關閉

#import "FlipsideViewController.h" 

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate,UIPopoverControllerDelegate> 

@property (strong, nonatomic) UIPopoverController *flipsidePopoverController; 

- (IBAction)showInfo:(id)sender; 

@end 
  1. 在MainViewController:
 

    - (IBAction)showInfo:(id)sender 
    { 
     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
      FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil]; 
      controller.delegate = self; 
      controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
      [self presentModalViewController:controller animated:YES]; 
     } else { 
      if (!self.flipsidePopoverController) { 
       FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil]; 
       controller.delegate = self; 

       self.flipsidePopoverController.delegate = self 

       self.flipsidePopoverController = [[UIPopoverController alloc] initWithContentViewController:controller]; 
      } 
      if ([self.flipsidePopoverController isPopoverVisible]) { 
       [self.flipsidePopoverController dismissPopoverAnimated:YES]; 
      } else { 
       [self.flipsidePopoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
      } 
     } 
    } 

    -(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController 
    { 
     NSLog(@"OLOLO"); 
    } 

但是當我點擊某處並且popover消失時,控制檯中沒有NSLog消息。 我在做什麼錯?

+4

要設置委託* *之前**你實例化popover控制器。換句話說,當您設置代表 – 2012-04-25 21:47:06

+0

真相,真相時,它不存在。非常感謝! – 2012-04-26 08:45:47

回答

2

您是否將您的MainViewController實例設置爲彈出窗口的代表?

(如果您創建通過代碼酥料餅) popover.delegate = self;

+0

FlipsideViewController * controller = [[FlipsideViewController alloc] initWithNibName:@「FlipsideViewController」bundle:nil]; controller.delegate = self; – 2012-04-25 16:24:51

+0

它已經在那裏。 – 2012-04-25 16:39:05

+0

這是你如何創建popover?發佈更多代碼 – 2012-04-25 16:49:43

15

設置酥料餅自我的代表,你也可以使用兩個酥料餅的代表,即: -

/* Called on the delegate when the popover controller will dismiss the popover. Return NO to prevent the dismissal of the view. 
*/ 
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController; 

/* Called on the delegate when the user has taken action to dismiss the popover. This is not called when -dismissPopoverAnimated: is called directly. 
*/ 
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController; 
+1

請記住,didDismiss函數僅在popover解散時自動調用,例如在它之外觸摸。如果你自己用自己的代碼解僱它(例如從一個按鈕按下),那麼它將不會被調用。 – CMash 2017-03-30 12:26:30

4

早些時候答案已經使用建議UIPopoverControllerDelegate似乎是實現目標的唯一明智的方式。我認爲增加一個實際的例子會很好,因爲這不是最簡單的事情。我的要求很簡單 - 我想是模糊的背景視圖而酥料餅是可見的,所以這裏的步驟:

  1. 線了一個故事板的酥料餅,配置它在目標視圖控制器酥料餅的內容大小屬性。

  2. 讓你的源視圖控制器打開.h文件,並做一些這樣的UIPopoverControllerDelegate:

    @interface MyController : UIViewController <UIPopoverControllerDelegate> 
    
  3. 覆蓋prepareForSegue,分配你的源視圖控制器爲代表的酥料餅,然後設置阿爾法0.5之前的SEGUE提出目標:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
        UIStoryboardPopoverSegue* popover = (UIStoryboardPopoverSegue*)segue; 
        popover.popoverController.delegate = self; 
        self.view.alpha = 0.5; 
    } 
    
  4. 落實酥料餅的委託方法popoverControllerDidDismissPopover。設置阿爾法回到1.0和取消自己作爲代表,以確保我們不阻止ARC做的工作:

    -(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController { 
        self.view.alpha = 1.0; 
        popoverController.delegate = nil; 
    } 
    
+1

請記住,didDismiss函數僅在popover解散時自動調用,例如在它之外觸摸。如果你自己用自己的代碼解僱它(例如從一個按鈕按下),那麼它將不會被調用。 – CMash 2017-03-30 12:26:51

0
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
UIStoryboardPopoverSegue *popoverSegue; 
popoverSegue = (UIStoryboardPopoverSegue *)segue; 
popoverController = popoverSegue.popoverController; 
pCVisible = YES; 
[[segue destinationViewController] setDelegate:self]; 
} 

- (void) setDataFromPopover { 
    if (pCVisible) { 
    [popoverController dismissPopoverAnimated:YES]; // THIS IS KEY! this is where the popover is dismissed, not in the popover itself 
    } 
} 

而且

//TableViewController.m 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:  (NSIndexPath *)indexPath 
{ 
    //variable = whatever 

OtherViewController *initialView; 
initialView=(OtherViewController *)self.delegate; 
initialView.theLabel.text = variable; 
[initialView setDataFromPopover]; 
}