在Interface Builder中为iPhone创建弹出视图控制器

3
我该如何做到这一点?我知道我必须给弹出框一个视图控制器,让它从中构建其视图,但我不知道如何在Interface Builder中构建此视图(例如添加标签和按钮等)。
我只需在普通的故事板中完成它,然后再创建一个与其他任何内容都没有连接的故事板,放在角落里吗?
我需要创建另一个故事板吗?
即使我的项目全部使用故事板,我是否需要创建一个xib?
我需要通过编程方式创建它吗?

你的应用程序中会从多个地方呈现它,还是只有一个地方? - jrturton
3个回答

8

由于您的问题中没有指定目标设备,因此我给出了iPad的答案。

iPad:

进入您的故事板,拖放一个viewcontrollertableviewcontroller,取决于您的需要。然后从您的故事板上所需的viewcontroller创建一个segue到新拖放的viewcontroller上。选择您的segue为popOver

enter image description here

enter image description here

请确保在转场设置中选择一个锚点,就像上面的图片一样。然后您需要编辑弹出窗口的大小。如果它是一个uiviewcontroller,请选择它的视图;如果它是一个tableviewcontroller,请选择界面构建器左侧的其表格视图并编辑其大小。

首先:

enter image description here

第二个:

enter image description here

接下来,定制您的弹出视图控制器(拖放的视图控制器),添加按钮、标签或任何您想要的内容。

如果您要在弹出视图中进行其他操作: 不要忘记创建新文件-> uiviewcontroller或uitableviewcontroller子类。然后将其与您在故事板上新创建的视图控制器关联起来。

enter image description here

IPHONE:

iPhone 中没有 Popover 控制器。

但是你可以尝试使用第三方 https://github.com/50pixels/FPPopover 解决方案,通过使用 QuartzCore 来模拟 iPhone 中的 Popover 行为。

我建议按照以下步骤尝试:

首先:

再次将 uiviewcontoller 拖放到你的故事板中,然后创建一个新文件,继承 uiviewcontroller

这个新的 uiviewcontroller 将坐落在故事板的角落。

然后将你的 uiviewcontroller 与新创建的文件关联起来,在你的故事板中选择它的类名,并赋予其一个 storyboard id。

enter image description here

-(IBAction)buttonClicked:(UIButton*)okButton
{
    //the view controller you want to present as popover
    YourViewController *controller = [[YourViewController alloc] init]; 

    //if [[YourViewController alloc] init]; doesn't work try this
   // UIStoryboard* sb = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                     bundle:nil];
    //YourViewController *controller = [sb instantiateViewControllerWithIdentifier:@"YourViewController"]; 

    //our popover
    FPPopoverController *popover = [[FPPopoverController alloc] initWithViewController:controller]; 

    //the popover will be presented from the okButton view 
    [popover presentPopoverFromView:okButton]; 

    //no release (ARC enable)
    //[controller release];
}

注意:我之前没有使用过FPPopover,因此不知道如何安排屏幕大小,但他们的文档中一定有进一步的解释,请仔细阅读。

非常好,谢谢。但是,如果我正在使用FPPopover库(iPad弹出窗口的iPhone端口),它只需要一个弹出窗口的视图,那么我是否可以创建一个普通的segue?如果您不知道,我完全理解,我误导地认为两者是相同的(大多数事情似乎都是)。 - Doug Smith
@DougSmith 试试我的 iPhone 答案,不确定是否有效。 - SpaceDust__
@SpaceDust 你好,我已经完成了弹出视图。问题是,我在弹出视图中有一个按钮。当我按下该按钮时,弹出视图应该被关闭。我已经尝试了一天,但是一直没有找到解决方法。如果你有任何想法,对我来说将非常有帮助。非常感谢! - vinothp
@Vino 在你的按钮方法中调用 [self dismissViewControllerAnimated:YES completion:NULL]; 如果这不起作用,请发布一个问题,我相信如果你展示你的代码,人们会回答它。 - SpaceDust__
@SpaceDust 感谢您的回复。我终于找到了。[self dismissViewControllerAnimated:YES completion:NULL]; 这个方法在弹出窗口中不起作用。但是 [self.popOverController dismissPopoverAnimated:YES]; 可以解决问题。 - vinothp
显示剩余2条评论

2

iPhone解决方案

自从iOS 8.0版本以后,也可以在iPhone应用中实现这一功能!

目标视图控制器(即应该出现在弹出窗口中的控制器)需要符合 'UIPopoverPresentationControllerDelegate' 协议,并且必须实现 'adaptivePresentationStyleForPresentationController:traitCollection:' 方法。此外,其 'modalPresentationStyle' 应为 'UIModalPresentationPopover',并且该控制器应成为自己的 'UIPopoverPresentationController' 委托。

准备好了视图控制器之后,只需从您的 UINavigationBarItem(或其他任何东西)到目标视图控制器创建一个故事板segue(类型:'Present As Popover')即可。

enter image description here

实施:

@implementation TestViewController

/*
 initWithCoder:

 */
- (instancetype)initWithCoder:(NSCoder *)pDecoder {
    //FLog;

    if (self = [super initWithCoder:pDecoder]) {

        self.modalPresentationStyle = UIModalPresentationPopover;
        self.popoverPresentationController.delegate = self;
    }
    return self;
}


#pragma mark - POPOVER PRESENTATION CONTROLLER DELEGATE

/*
 adaptivePresentationStyleForPresentationController:traitCollection:

 */
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)pController
                                                               traitCollection:(UITraitCollection *)pTraitCollection {
    //FLog;

    return UIModalPresentationNone;
}

// ...

@end

当然,委托协议也可以在其他类中实现,但这种方式是最简单的 :-)

1
我认为iPhone设备的更好方式是创建一个视图控制器,其视图的背景颜色设置为透明。然后在此视图中只需添加较小尺寸的视图,这将呈现出弹出窗口的外观。
呈现此视图控制器将呈现为弹出窗口的外观。这是我采取的方法。如果用户点击较小视图范围之外的父视图区域,则可以关闭视图控制器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接