Segues initiated directly from view controllers warning in storyboard xcode

user1344851 picture user1344851 · Jun 21, 2012 · Viewed 24.4k times · Source

Hi I have created button programmatically and I connected it to another view, but I got segue warning

that I should use prepareForSegue method for storyboard but I don't know how, there are some sample on Internet but I get an error when I used that sample, would you please help me

here is my code

Creating Button

 UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; 
 button.backgroundColor=[UIColor colorWithRed: 201.0/255.0 green: 201.0/255.0 blue:201.0/255.0 alpha: 1.0];
 button.tag = currentTag;
 currentTag++;
 [button.layer setBorderColor: [[UIColor blackColor] CGColor]];
 [button.layer setBorderWidth: 1.0];
 [button setTitle:[NSString stringWithFormat:@"%d",currentTag] forState:UIControlStateNormal];
 button.frame = CGRectMake(80*x, 32*y, 80, 32); 
 [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
 [buttonView addSubview: button];

Action for button

-(void)buttonPressed:(UIButton *)button
{
    NSLog(@"button %u -- frame: %@", button.tag, NSStringFromCGRect(button.frame));
    [self performSegueWithIdentifier:@"WeekView" sender:self];    
}

Prepare for segue MY Warning

Segues initiated directly from view controllers must have an identifier for use with -[UIViewController performSegueWithIdentifier:sender:]

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"WeekView"]) {
        [segue.destinationViewController setTitle:@"WeekView"];
    }
}

Answer

Travis M. picture Travis M. · Oct 11, 2012

Segues initiated directly from view controllers must have an identifier for use with -[UIViewController performSegueWithIdentifier:sender:]

This warning occurs when you drag a segue directly from a View Controller in Storyboard (aka, not from a button or any action control) and don't type in an Identifier (Name).

You have to give these ones a Identifier or else there's no way to call them programmatically... which is the only reason why you would link a segue like this.

In Xcode 8, you should be able to double click the warning to have the storyboard bring up the offending segue so that you can add a name to it.