以编程方式添加按钮以执行 segue

Adding button programmatically to perform segue(以编程方式添加按钮以执行 segue)
本文介绍了以编程方式添加按钮以执行 segue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用代码即时"创建一个按钮,并且我希望该按钮能够执行转到不同的控制器.据我所知,以编程方式创建 segue 是不可能的,但是如果创建的按钮不在我的故事板上,我怎么能通过呢?

I am trying to create a button 'on the fly' using code, and i want that button to perform segue to a different controller. As far as i know, creating segue programmatically is not possible, but how can i get pass that if the created button is not on my storyboard?

这是创建按钮的代码:

- (void)addButton:(UIView*)view inLocation:(CGPoint)point
{

    UIButton *but=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    but.frame= CGRectMake(point.x, point.y,30,30);
    [but setTitle:@"CHOOSE" forState:UIControlStateNormal];
    // The nilSymbol should call a method that will perform the segue
    [but addTarget:self action:@selector(nilSymbol) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:but];  
}

有什么选择吗?

推荐答案

在 Interface Builder 中,创建一个 segue.由于您无法将 segue 连接到按钮(它还不存在),只需将 segue 连接到起始 ViewController.然后,当您创建按钮时:

In Interface Builder, create a segue. Since you can't connect the segue to the button (it doesn't exist yet), simply connect the segue to the starting ViewController. Then, when you create the button:

// ....
[but addTarget:self action:@selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
// ....

而在一些方法中:

-(void)someMethod
{
    [self performSegueWithIdentifier:@"segueIdentifier" sender:self];
}

这篇关于以编程方式添加按钮以执行 segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)