fredley
fredley

Reputation: 33941

prepareforsegue enters infinite loop

I am saving an image in my ViewController, and want to pass it to the next one during segue, however doing this causes an infinite loop.

Here is my code:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"next"])
    {
        ReviewViewController *rvc = [segue destinationViewController];
        [rvc setFilename:self.filename];
    }
}

the setFilename method in ReviewViewController simply looks like this:

- (void)setFilename:(NSString *)filename{
    NSLog([NSString stringWithFormat:@"Setting filename to: %@",filename]);
    self.filename = filename;
}

That log statement fires off ad-infinitum when I add in the prepareForSegue method. If I remove it, the transition is fine.

What is going on?

Upvotes: 0

Views: 75

Answers (1)

fredley
fredley

Reputation: 33941

The answer, as many pointed out in the comments, is that self.filename = actually calls [self setFilename], which is what I had called my method.

This is fixed by either renaming my setFilename method, or doing the assignment via _filename = instead.

Upvotes: 2

Related Questions