Adding button programmatically to perform 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:以编程方式添加按钮以执行 segue
基础教程推荐
- 如何从 logcat 中删除旧数据? 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
