iOS:如何消除弹出式窗口 [英] iOS: how to dismiss popovers

查看:782
本文介绍了iOS:如何消除弹出式窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何关闭分镜脚本弹出框

Possible Duplicate:
How to Dismiss a Storyboard Popover

我有一个iPad故事板.一个视图中的Bar Button项被Control-Drag拖到另一个视图中,并且选择了弹出样式.当我按下按钮时,新的弹出视图会显示在弹出窗口中,但是我有两个问题:

I have a iPad storyboard. A Bar Button Item in one View is Control-dragged to another view -- and a popover style is chosen. When I press the button the new popover view is shown in a popover, but I have two problems:

  1. 当我再次按下按钮时,将显示弹出窗口视图的另一个实例-我可以看到,黑色边框越来越暗.如果弹出视图打开并且按了按钮,则弹出视图应该关闭.我该怎么办?
  2. 当前,即使视图中的内容不是那么高,新的Popover视图仍浮动在屏幕的按钮上.如何控制弹出窗口的尺寸?

我通过从控制器底部的黄色控制器图标拖动到另一个视图(该视图应该在弹出框内)来创建序列.此弹出框的标识符是settingsPopover.

I've created a segue by dragging from the yellow controller icon in the bottom of the controller to the other view, which should be inside the popover. The identifier for this popover is settingsPopover.

然后我在IBAction中执行此操作:

I then do this inside the IBAction:

- (IBAction)settingsButtonTapped:(id)sender {
        [self performSegueWithIdentifier:@"settingsPopover" sender:self];
}

但这给了我这个错误:

由于未捕获的异常而终止应用程序 "NSInternalInconsistencyException",原因:"UIStoryboardPopoverSegue" 必须从条形按钮项或视图中显示."

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIStoryboardPopoverSegue must be presented from a bar button item or a view.'

我是否以错误的方式创建了segue或在调用performSegueWithIdentifier时出错?

Have I created the segue in a bad way or in the call to performSegueWithIdentifier wrong?

我已经创建了这个IBAction:

I've created this IBAction:

- (IBAction)settingsButtonTapped:(id)sender {
    if (_settingsPopover == nil) {
        SettingsViewController* settingsView = [[SettingsViewController alloc] initWithStyle:UITableViewStylePlain];
        self.settingsPopover = [[UIPopoverController alloc] initWithContentViewController:settingsView];
    }
    [self.settingsPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

它几乎可以工作,但是一个缺点是它没有使用我的情节提要中设计的UIView.例如,在我的情节提要中,具有类SettingsViewController的视图设计为分组表视图.有没有办法显示在情节提要中的情节提要中设计的UIView,而不是原始的SettingsViewController实例?

It almost work, but one drawback is that it is not using the UIView designed in my Storyboard. For instance, in my storyboard the view that has the class SettingsViewController in designed as a grouped table view. Is there a way to display the UIView designed in the storyboard inside the popover instead of the raw SettingsViewController instance?

解决方案:

我在情节提要中创建了一个全局标识,标识为"settingsPopover".

I created a global segue in the Storyboard with the identifier "settingsPopover".

- (IBAction)settingsButtonTapped:(id)sender {
    if (self.settingsPopover==nil) {
        [self performSegueWithIdentifier:@"settingsPopover" sender:sender];
    }
}

#pragma mark - UIView
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    
    if ([segue.identifier isEqualToString:@"settingsPopover"]) {
        self.settingsPopover = [(UIStoryboardPopoverSegue *)segue popoverController];
        self.settingsPopover.delegate = self;
    }
}

#pragma mark - UIPopoverControllerDelegate
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    self.settingsPopover = nil;
}

推荐答案

每次执行Segue时,它都会创建一个新的viewController,正如您看到的将按钮连接起来以执行segue时所看到的那样.因此,您需要通过从底部的viewController图标拖动到目标,为viewController创建一个通用"序列.然后,正如LucasTizma建议的那样,将按钮挂到IBAction上,该IBAction调用performSegueWithIdentifier:或关闭弹出窗口,这取决于弹出窗口是否在屏幕上.

Every time a Segue is performed it creates a new viewController, as you are seeing when you wired your button up to perform the segue. So instead you need to create a "generic" segue for the viewController by dragging from the viewController icon at the bottom to the destination. Then as LucasTizma suggested hook the button up to an IBAction that either calls performSegueWithIdentifier: or dismisses the popover, conditional on whether the popover is on screen or not.

这篇关于iOS:如何消除弹出式窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆