简体   繁体   中英

calling a method in another view controller is causing a loop

i'm transitioning to a new view, i present the new view and then call a method to load some data:

-(IBAction)switchToScoutingReport:(id)sender
{    
    InspectAppDelegate *dataCenter = (InspectAppDelegate *) [[UIApplication sharedApplication] delegate];

    [self saveData];

    ScoutingReportViewController *scoutingReport = [self.storyboard instantiateViewControllerWithIdentifier:@"ScoutView"];

    [self setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentModalViewController:scoutingReport animated:YES];
    [scoutingReport setScoutingEventData:[dataCenter.eventsArray objectAtIndex:0]];
}

in my scouting report view controller, it calls this method:

-(void)setScoutingEventData:(ScoutingEventData *)scoutingEventDataInput
{
    NSLog(@"setting event data");

    self.scoutingEventData = scoutingEventDataInput;
    [self loadDataWithEvent];
}

and for some reason, this is my output:

2011-12-22 10:17:20.637 Inspect[329:207] setting event data
2011-12-22 10:17:20.637 Inspect[329:207] setting event data
2011-12-22 10:17:20.638 Inspect[329:207] setting event data
2011-12-22 10:17:20.638 Inspect[329:207] setting event data
2011-12-22 10:17:20.639 Inspect[329:207] setting event data
2011-12-22 10:17:20.640 Inspect[329:207] setting event data
2011-12-22 10:17:20.640 Inspect[329:207] setting event data
2011-12-22 10:17:20.641 Inspect[329:207] setting event data

which is stuck in an infinite loop.

i have no idea why its looping, there is no loop code to do it. is it something special about using a "set" method? i do use @property and @synthesize to create scoutingEvenData. now that i think of it, i can just set scoutingEvenData in my other view controller.

but still interested in why this code creates a loop. any ideas?

EDIT: the setScoutingEventData method is called and loops when i call loadDataWithEvent from the other class (setScoutingEventData is never directly called in either controllers).... :/

You problem is obvious you are calling the setter within the setter, thus your "infite loop" you have

-(void)setScoutingEventData:(ScoutingEventData *)scoutingEventDataInput
{
    NSLog(@"setting event data");
   //here you are setting scotingEventData which calls this same method!
    self.scoutingEventData = scoutingEventDataInput;
    [self loadDataWithEvent];
}

Assuming you have some backing ivar, lets call it _scoutingEventData then your code should actually look like

-(void)setScoutingEventData:(ScoutingEventData *)scoutingEventDataInput
{
    NSLog(@"setting event data");
   //here you are setting scotingEventData which calls this same method!
     _scoutingEventData = scoutingEventDataInput;
    [self loadDataWithEvent];
}

Point: when you have a property calling self.property will trigger the setProperty method...since you are calling it within the setter the method just keeps calling itself over and over again..youd probably get a stackoverflow eventually...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM