xcode 如何使用Storyboard制作可以在代码中使用的popover?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11161909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Storyboard to make popover that can be used in code?
提问by David U
I'm building a collection of forms each of which contains several fields. Some of the fields are UITextField
s 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
.
我正在构建一个表单集合,每个表单都包含几个字段。一些字段是UITextField
将显示日期的 s。我创建了一个名为 的新类DatePickerTextField
,它是UITextField
. 当点击 a 时DatePickerTextField
,我希望UIDatePicker
控件出现在popover
.
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?
我的问题是如何使用故事板来实现popover
?当场景中有一个特定的、可见的控件时,我可以做一个 segue。但是我如何popover
在场景中表示一个泛型,我可以附加到任何DatePickerTextField
激活的实例化?
回答by Johnnywho
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 Sizeto Freeform, Status Barto None, specify unique Identifierthat 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.
您可以创建未连接到任何控件的 segue,但我认为没有办法从代码中为 popover 指定锚点。另一种选择是创建不与任何 segue 连接的 ViewController。编辑故事板时,创建将放置在弹出窗口中的 ViewController,选择它并导航到Utilities 窗格-> Attributes Inspector。将Size设置为Freeform,将Status Bar 设置为None,指定将用于从代码实例化 ViewController 的唯一标识符。现在您可以通过选择 ViewController 并导航到Utilities 窗格-> Size Inspector来更改 ViewController 的大小.
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];
}
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:
一个警告是,您需要将 popover 的引用作为类的 ivar 保留,否则它会崩溃,因为 UIPopoverController 将在buttonPressed
返回后被释放和解除分配:
@interface MyViewController : UIViewController {
// ...
UIPopoverController *popover;
// ...
}
回答by Thomas Lechner
So, I had a similar issue, and in case others might benefit, I figured I'd share it, since I benefit so much from stackoverflow.
所以,我有一个类似的问题,如果其他人可能会受益,我想我会分享它,因为我从 stackoverflow 中受益匪浅。
This solution allows you to set the anchor of a customizable popover segue. It also allows you to configure the segue to be modal or not (I could not find a way to prevent the segue by dimming the exterior context, so if someone knows how to do that, I would be interested in hearing it); this is accomplished by setting the passthrough views for the popover controller. I also added the capacity to specify a custom view, rather than the view of the source viewcontroller (since I needed this capacity); this portion is not critical to the solution.
此解决方案允许您设置可自定义的 popover segue 的锚点。它还允许您将 segue 配置为模态与否(我找不到通过使外部上下文变暗来防止 segue 的方法,所以如果有人知道如何做到这一点,我会很想听听它);这是通过为 popover 控制器设置直通视图来实现的。我还添加了指定自定义视图的容量,而不是源视图控制器的视图(因为我需要这个容量);这部分对解决方案并不重要。
DynamicPopoverSegue.h
DynamicPopoverSegue.h
@interface DynamicPopoverSegue : UIStoryboardPopoverSegue
@property BOOL isModal;
@property UIView* sourceView;
@property CGRect anchor;
@end
DynamicPopoverSegue.m
DynamicPopoverSegue.m
@implementation DynamicPopoverSegue
- (void)perform
{
if (!self.popoverController.popoverVisible)
{
UIViewController* dst = (UIViewController*)self.destinationViewController;
UIViewController* src = (UIViewController*)self.sourceViewController;
UIView* inView = _sourceView ? _sourceView : src.view;
self.popoverController.contentViewController = dst;
if (!_isModal)
{
[self.popoverController setPassthroughViews:[[NSArray alloc] initWithObjects:inView, nil]];
}
[self.popoverController presentPopoverFromRect:_anchor
inView:inView
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
}
@end
Then you just set your segue to "Custom" in the storyboard, and set the segue class to "DynamicPopoverSegue". In my case, since I wanted to associate it with dynamic layers in a view, I could not set the anchor, so I created the segue by control clicking from the view controller icon in the bar beneath my view controller to the view controller I was using to present the popupover.
然后,您只需在情节提要中将 segue 设置为“Custom”,并将 segue 类设置为“DynamicPopoverSegue”。就我而言,由于我想将其与视图中的动态层相关联,因此无法设置锚点,因此我通过从视图控制器下方栏中的视图控制器图标到我所在的视图控制器的控件单击来创建转场用于呈现弹出窗口。
To call the popover segue:
调用popover segue:
[self performSegueWithIdentifier:@"MyPopoverSegue" sender:self];
And to configure the popover segue:
并配置popover segue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"MyPopoverSegue"])
{
DynamicPopoverSegue* popoverSegue = (DynamicPopoverSegue*)segue;
// set the anchor to wherever you want it to be
popoverSegue.anchor = _destinationFrame;
}
}
回答by Gank
- (IBAction)pressItemChooseOprateRoom:(id)sender {
if (isPad){
// UIView *anchor = sender;
UIViewController *viewControllerForPopover =
[self.storyboard instantiateViewControllerWithIdentifier:@"OperateRoomList"];
_myPopover = [[UIPopoverController alloc]
initWithContentViewController:viewControllerForPopover];
CGRect rc=[self getBarItemRc:sender];
[_myPopover presentPopoverFromRect:rc
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[MLControl shared].popover =self;
// [self perfformSegueWithIdentifier:SEGUE_POP_OPERATEROOM sender:self];
}else{
[self iphoneOpenOperateRoomList];
/* [self performSegueWithIdentifier:@"iPhonePushOperateRoom" sender:self];
*/
}
}
-(void)iphoneOpenOperateRoomList{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"OperateRoomList"];
// if (!index.showTabBar) {
// vc.hidesBottomBarWhenPushed = YES;
// }
[self.navigationController pushViewController:vc animated:YES];
}
回答by Stefan
Just used the answer from Jonnywho for my SWIFT project. In case you need it:
刚刚在我的 SWIFT 项目中使用了 Jonnywho 的答案。如果您需要它:
Here's the SWIFT version:
这是 SWIFT 版本:
let anchor: UIView = sender
var viewControllerForPopover = self.storyboard?.instantiateViewControllerWithIdentifier("GameAboutViewController") as! UIViewController?
let popover = UIPopoverController(contentViewController: viewControllerForPopover!)
popover.presentPopoverFromRect(anchor.frame, inView: anchor, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
回答by bhagyash ingale
Add a UIView in the scene dock.
在场景停靠栏中添加一个 UIView。
You can add it as a subview to any existing view on the view controller.
您可以将其作为子视图添加到视图控制器上的任何现有视图。
You can then toggle it's isHidden property as you require.
然后,您可以根据需要切换它的 isHidden 属性。
You can add multiple such subviews and create many such popups.
您可以添加多个此类子视图并创建许多此类弹出窗口。
This technique will save you from setting up a new View Controller and using segues.
这种技术将使您免于设置新的视图控制器和使用 segue。