以编程方式添加按钮以执行 segue [英] Adding button programmatically to perform segue

查看:18
本文介绍了以编程方式添加按钮以执行 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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