iPad:如何通过按下自己的“完成”按钮来关闭UIPopoverController

时间:2013-06-17 18:34:35

标签: uipopovercontroller

我的popover有自己的“完成”按钮,我想通过触摸此选项来解除我的popover,但仍然无法弄清楚如何,这是应用程序的捕获

screen capture

创建代码(在名为VisitVC的rootview控制器内)

- (IBAction)showStartEndEvent:(id)sender {

    if ([[UIDevice currentDevice].model hasPrefix:@"iPhone"])
        [self performSegueWithIdentifier:@"viewSceneStartEndEvent" sender:self];

    else
    {
        //iPad Popover Section
        if (!startEndEventPopover || !startEndEventPopover.popoverVisible)
        {
            StartEndEventVC *controller     = [self.storyboard instantiateViewControllerWithIdentifier:@"StartEndEventVC"];

            controller.FecInicioDateSE      = self.fecInicioDateV;
            controller.fecFinDateSE         = self.fecFinDateV;

            navController               = [[UINavigationController alloc]initWithRootViewController:controller];
            navController.toolbarHidden = FALSE;

            startEndEventPopover        = [[UIPopoverController alloc] initWithContentViewController:navController];

            [startEndEventPopover presentPopoverFromRect:CGRectMake(400, [sender convertPoint:[sender bounds].origin toView:self.view].y, 300, 400) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
            }
        else{
            [startEndEventPopover dismissPopoverAnimated:YES];
            startEndEventPopover = nil;
        }
    }
}

现在popover控制器(StartEndEventVC)中的“完成”按钮代码

- (IBAction)saveChanges:(id)sender {

    if (fechaInValida) {
        [[[UIAlertView alloc]initWithTitle:@"Notificación" message:@"La fecha de inicio debe ser anterior a la fecha de finalización" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
        return;
    }

    [self comparaFechaInicio];


    [self.startDateLabel    removeObserver:self forKeyPath:@"text"];
    [self.endDateLabel      removeObserver:self forKeyPath:@"text"];

    [CurrentParentViewController setFecFinDateV:self.fecFinDateSE];
    [CurrentParentViewController setFecInicioDateV:self.fecInicioDateSE];


    if ([[UIDevice currentDevice].model hasPrefix:@"iPhone"])
        [self.navigationController popViewControllerAnimated:YES];
    else{
        [self.navigationController dismissViewControllerAnimated:YES completion:^{}];
    }
}

解除弹出窗口的代码键位于块上:

if ([[UIDevice currentDevice].model hasPrefix:@"iPhone"])
    [self.navigationController popViewControllerAnimated:YES];
else{
    **[self.navigationController dismissViewControllerAnimated:YES completion:^{}];**
}

但不知道如何,任何想法???

2 个答案:

答案 0 :(得分:2)

要解除弹出框,您必须将dismissPopoverAnimated:发送到弹出控制器。要实现这一点,您应该保留对弹出窗口的引用,然后从弹出控制器中显示的视图控制器通知视图控制器,您将通过按下按钮来显示弹出窗口,此时您将调用{{1 }}。您可以使用委托或块来通知按下按钮。

答案 1 :(得分:1)

我做到了!使用委托,我所要做的就是为我的popover类实现一个委托(startEndEvent.h)

@class StartEndEventVC;

@protocol StartEndEventVCDelegate

@optional -(void)startEndEventsVC:(StartEndEventVC *)sender;

@end

@interface StartEndEventVC : UITableViewController

@property (strong, nonatomic)           id <StartEndEventVCDelegate> delegate;

@end

在我的保存数据方法中,我实现了委托,就像这样......

- (IBAction)saveChanges:(id)sender {

    if (fechaInValida) {
        [[[UIAlertView alloc]initWithTitle:@"Notificación" message:@"La fecha de inicio debe ser anterior a la fecha de finalización" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
        return;
    }

    [self comparaFechaInicio];

    if ([[UIDevice currentDevice].model hasPrefix:@"iPhone"]){
        [self.startDateLabel    removeObserver:self forKeyPath:@"text"];
        [self.endDateLabel      removeObserver:self forKeyPath:@"text"];

        [CurrentParentViewController setFecFinDateV:self.fecFinDateSE];
        [CurrentParentViewController setFecInicioDateV:self.fecInicioDateSE];

        [self.navigationController popViewControllerAnimated:YES];
    }

    else{
        [self.delegate startEndEventsVC:self];
    }
}

最后在我的rootViewController的类(VisitVC.m)上我采用了我的自定义StartEndEventVCDelegate

@interface VisitVC ()<StartEndEventVCDelegate>
...
@end

我设置了popover的委托并实现了popover的委托方法

- (IBAction)showStartEndEvent:(id)sender {

    if ([[UIDevice currentDevice].model hasPrefix:@"iPhone"])
        [self performSegueWithIdentifier:@"viewSceneStartEndEvent" sender:self];

    else
    {
        //iPad Popover Section
        if (!startEndEventPopover || !startEndEventPopover.popoverVisible)
        {
            StartEndEventVC *controller     = [self.storyboard instantiateViewControllerWithIdentifier:@"StartEndEventVC"];

            controller.FecInicioDateSE      = self.fecInicioDateV;
            controller.fecFinDateSE         = self.fecFinDateV;

            controller.delegate = self;

            navController               = [[UINavigationController alloc]initWithRootViewController:controller];
            navController.toolbarHidden = FALSE;

            startEndEventPopover        = [[UIPopoverController alloc] initWithContentViewController:navController];

            [startEndEventPopover presentPopoverFromRect:CGRectMake(400, [sender convertPoint:[sender bounds].origin toView:self.view].y, 300, 400) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
            }
        else{
            [startEndEventPopover dismissPopoverAnimated:YES];
            startEndEventPopover = nil;
        }
    }
}

-(void)startEndEventsVC:(StartEndEventVC *)sender{    
    [startEndEventPopover dismissPopoverAnimated:YES];
}

它是...如果你有一个较小的解决方案是受欢迎的,但它的工作正常:)