Itzik984
Itzik984

Reputation: 16764

Adding button programmatically to perform 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?

This is the code for creating the button:

- (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];  
}

Any options to do that?

Upvotes: 1

Views: 8558

Answers (1)

LombaX
LombaX

Reputation: 17364

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];
// ....

And in the someMethod:

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

Upvotes: 8

Related Questions