Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

ios - Segues initiated directly from view controllers warning in storyboard xcode

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"];
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...