我有一个自定义的 UIButton
,我是这样编程定义的:
self.hard1 = [UIButton buttonWithType:UIButtonTypeCustom];
[self.hard1 setFrame:CGRectMake(884, 524, 105, 60)]; // set the x,y,width and height
UIImage *buttonImage = [UIImage imageNamed:@"green.jpg"];
self.hard1.layer.cornerRadius = 10;
self.hard1.clipsToBounds = YES;
[self.hard1 addTarget: self
action: @selector(buttonTapped:)
forControlEvents: UIControlEventTouchUpInside];
[self.hard1 setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:self.hard1];
因此,它不会显示在 Interface Builder 中,它只会在我运行应用程序时出现在屏幕上。这意味着我无法按住 ctrl 并从 UIButton
拖动到 ViewController
以选择弹出框 segue。我可以在我的代码中调用 segue 吗?如果没有,我还有其他选择吗?
最佳答案
Blub 和 tkanzakic 的答案不太奏效,因为当您创建 segue 时,popover segue 坚持要在 Storyboard 中有一个 anchor View 。由于您的按钮尚不存在,因此很难做到这一点。您可以锚定到现有的 Storyboard View ,但弹出箭头在弹出时不会指向正确的对象。您可以在代码中移动现有 View 以匹配代码创建的按钮的矩形。但是您也可以完全放弃 segue 并在代码中执行弹出窗口。
您需要声明一个 popoverController 属性:
@property (nonatomic, strong) UIPopoverController* buttonPopoverController;
然后你的按钮 Action 看起来像这样:
- (void) buttonTapped:(UIButton*) sender
{
ContentViewController* contentVC = [[ContentViewController alloc] init];
self.buttonPopoverController = [[UIPopoverController alloc]
initWithContentViewController:contentVC];
self.buttonPopoverController.delegate = self;
//only required if using delegate methods
[self.buttonPopoverController presentPopoverFromRect:sender.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
ContentViewController 是您打算连接到的任何 View Controller 。如果它是使用 Storyboard 场景配置的,您可能希望在创建它时执行如下操作:
UIStoryboard *storyboard = self.storyboard;
ContentViewController* contentVC =
[storyboard instantiateViewControllerWithIdentifier:@"ContentViewController"];
当您在 Storyboard中选择了相关的 View Controller 时,您可以使用身份检查器设置 Storyboard标识符。
关于iphone - 为自定义 UIButton 编写 Popover segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15093485/