如何使用Storyboard制作可以在代码中使用的弹出窗口? [英] How to use Storyboard to make popover that can be used in code?

查看:368
本文介绍了如何使用Storyboard制作可以在代码中使用的弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个表单集合,每个表单包含几个字段.有些字段是UITextField,将显示日期.我创建了一个名为DatePickerTextField的新类,它是UITextField的后代.点击DatePickerTextField时,我希望UIDatePicker控件出现在popover中.

I'm building a collection of forms each of which contains several fields. Some of the fields are UITextFields that will display a date. I've created a new class called DatePickerTextField, a descendant of UITextField. When a DatePickerTextField is tapped I'd like for a UIDatePicker control to appear in a popover.

我的问题是我如何使用情节提要实现popover?当场景中有特定的可见控件时,我可以进行查找.但是,如何在场景中表示通用的popover,我可以将其附加到任何已激活的实例化DatePickerTextField上?

My question is how do I use the storyboard to implement the popover? I can do a segue when there is a specific, visible control in the scene. But how do I represent a generic popover in the scene that I can attach to any instantiated DatePickerTextField that becomes active?

推荐答案

您可以创建未连接到任何控件的segue,但我认为没有办法为代码中的弹出窗口指定定位点.另一个选择是创建不与任何segue连接的ViewController.编辑情节提要时,创建将放置在弹出窗口中的ViewController,选择它,然后导航到实用工具"窗格-> Attributes Inspector .将大小设置为自由格式,将状态栏设置为,并指定唯一的标识符用于从代码实例化ViewController.现在,您可以通过选择ViewController的View并导航到 Utilities窗格-> Size Inspector 来更改ViewController的大小.

You can create segue that is not connected to any control but I don't think that there would be way to specify anchor point for popover from code. Another option is to create ViewController that is not connected with any segue. When editing storyboard, create ViewController which will be placed in popover, select it and navigate to Utilities pane->Attributes Inspector. Set Size to Freeform, Status Bar to None, specify unique Identifier that will be used to instantiate ViewController from code. Now you can change the size of ViewController by selecting its View and navigating to Utilities pane->Size Inspector.

之后,您可以从代码中创建弹出窗口:

After that you can create popover from code:

- (IBAction)buttonPressed:(id)sender {
    UIView *anchor = sender;
    UIViewController *viewControllerForPopover = 
        [self.storyboard instantiateViewControllerWithIdentifier:@"yourIdentifier"];

    popover = [[UIPopoverController alloc] 
               initWithContentViewController:viewControllerForPopover];
    [popover presentPopoverFromRect:anchor.frame 
                             inView:anchor.superview 
           permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

一个警告是您需要将popover的引用作为类的ivar保留,否则会崩溃,因为buttonPressed返回后会释放并释放UIPopoverController:

One caveat is that you need to hold reference to popover as ivar of your class, otherwise it'll crash because UIPopoverController would be released and deallocated after buttonPressed returns:

@interface MyViewController : UIViewController {
//  ...
    UIPopoverController *popover;
//  ...
}

这篇关于如何使用Storyboard制作可以在代码中使用的弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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