以编程方式添加button以执行segue

我正在尝试使用代码创build一个button“on the fly”,并且我希望该button执行到不同的控制器的继续。 据我所知,以编程方式创buildsegue是不可能的,但如果创build的button不在我的故事板上,我怎么能通过?

这是创buildbutton的代码:

- (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中,创build一个segue。 由于您无法将segue连接到button(它还不存在),只需将segue连接到起始ViewController即可。 然后,当你创buildbutton时:

 // .... [but addTarget:self action:@selector(someMethod) forControlEvents:UIControlEventTouchUpInside]; // .... 

而在某些方法中:

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