检测popover dismiss

时间:2012-04-25 16:02:44

标签: ios uipopovercontroller

我想玩UIPopupController,我明白我无法检测到我的popover何时解雇。 我的步骤:
1.从XCode创建示例(文件 - >新项目 - >实用应用程序)
2.添加到MainViewController.h UIPopoverControllerDelegate

#import "FlipsideViewController.h"

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate,UIPopoverControllerDelegate>

@property (strong, nonatomic) UIPopoverController *flipsidePopoverController;

- (IBAction)showInfo:(id)sender;

@end
  1. 在MainViewController中:
  2. 
        - (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消失时,Console中没有NSLog消息。 我做错了什么?

4 个答案:

答案 0 :(得分:15)

将popover的委托设置为self,你也可以使用两个popover代理,即: -

 /* 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 :(得分:5)

早期的答案建议使用UIPopoverControllerDelegate,这似乎是实现目标的唯一合理方式。我认为添加一个实际的例子会很好,因为它不是最简单的事情。我的要求很简单 - 我希望背景视图模糊,而弹出窗口是可见的,所以这里是步骤:

  1. 在故事板中连接您的popover,在目标视图控制器属性中配置它的弹出窗口内容大小。

  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;
    }
    

答案 2 :(得分:2)

您是否将MainViewController实例设置为popover的委托?

(如果你通过代码创建popover) popover.delegate = self;

答案 3 :(得分: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];
}