简体   繁体   English

在另一个视图控制器中调用方法会导致循环

[英]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. 我确实使用@property和@synthesize创建scoutingEvenData。 now that i think of it, i can just set scoutingEvenData in my other view controller. 现在我想到了,我可以在其他视图控制器中设置scoutingEvenData。

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).... :/ 编辑:setScoutingEventData方法被调用并在我从另一个类调用loadDataWithEvent时循环(setScoutingEventData从未在任何一个控制器中直接调用)....:/

You problem is obvious you are calling the setter within the setter, thus your "infite loop" you have 您的问题很明显是您在setter中调用setter,因此您的“ infite loop”

-(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 假设您有一些支持ivar,将其命名为_scoutingEventData,那么您的代码实际上应该像

-(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... 指向:当您有一个调用self.property的属性时,将触发setProperty方法...由于您是在setter中调用它,因此该方法只会不断地反复调用自身。.您最终可能会得到stackoverflow ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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