我正在尝试使用代码“即时”创建一个按钮,并且我希望该按钮能够执行
转到不同的 Controller 。据我所知,以编程方式创建 segue 是不可能的,但是如果创建的按钮不在我的 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。
然后,当您创建按钮时:
// ....
[but addTarget:self action:@selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
// ....
在 someMethod 中:
-(void)someMethod
{
[self performSegueWithIdentifier:@"segueIdentifier" sender:self];
}
关于ios - 以编程方式添加按钮以执行 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21241194/