7

我想玩 UIPopupController,我知道我无法检测到我的弹出框何时关闭。我的步骤:
1. 从 XCode 创建示例(文件 -> 新项目 -> 实用程序)
2. 添加到 MainViewController.hUIPopoverControllerDelegate

#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 = UIModalTransitionStyleFlipHorizo​​ntal;
            [自我presentModalViewController:控制器动画:YES];
        } 别的 {
            如果(!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.flipsidePopoverControllerdismissPopoverAnimated:YES];
            } 别的 {
                [self.flipsidePopoverController presentPopoverFromBarButtonItem:sender allowedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
            }
        }
    }

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

但是当我点击某个地方并且弹出框消失时,控制台中没有 NSLog 消息。我究竟做错了什么?

4

4 回答 4

15

将弹出框的代表设置为 self ,您也可以使用两个弹出框代表,即:-

 /* 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;
于 2012-11-30T11:03:10.153 回答
5

较早的答案建议使用 UIPopoverControllerDelegate ,这似乎是实现目标的唯一明智方法。我认为添加一个实际示例会很好,因为这不是最简单的事情。我的要求很简单 - 我希望在弹出框可见的同时模糊背景视图,因此步骤如下:

  1. 在情节提要中连接您的弹出框,在目标视图控制器属性中配置它的弹出框内容大小。

  2. 通过打开 .h 文件并执行以下操作,使您的源视图控制器成为 UIPopoverControllerDelegate:

    @interface MyController : UIViewController <UIPopoverControllerDelegate>
    
  3. 覆盖 prepareForSegue,将源视图控制器指定为弹出框的委托,然后在 segue 呈现目的地之前将 alpha 设置为 0.5:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        UIStoryboardPopoverSegue* popover = (UIStoryboardPopoverSegue*)segue;
        popover.popoverController.delegate = self;
        self.view.alpha = 0.5;
    }
    
  4. 实现popover委托方法popoverControllerDidDismissPopover。将 alpha 设置回 1.0 并取消将自己指定为委托,以确保我们不会阻止 ARC 完成它的工作:

    -(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
        self.view.alpha = 1.0;
        popoverController.delegate = nil;
    }
    
于 2014-08-14T13:09:23.240 回答
2

Did you set your MainViewController instance as the delegate of the popover?

(if you create the popover through code) popover.delegate = self;

于 2012-04-25T16:11:32.137 回答
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];
}
于 2015-08-20T15:43:11.493 回答