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

查看:114
本文介绍了以编程方式添加按钮以执行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];
// ....

在someMethod中:

And in the someMethod:

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

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

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