以编程方式添加按钮以执行segue
问题描述:
我正尝试使用代码创建一个按钮'on the fly',并且我希望该按钮可以执行 segue到不同的控制器。据我所知,以编程方式创建segue是不可能的,但如果创建的按钮不在我的故事板上,我怎么能通过?以编程方式添加按钮以执行segue
这是创建按钮的代码:
- (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];
}
我真的不认为这是相同的情况下...他那边一个选择,我目前被迫向故事板中添加一个按钮而不是以编程方式添加它 – Itzik984