2014-10-20 64 views
0

所以我有我的AppDelegate方法试图设置一个对象在ViewController方法。我的AppDelegate看起来是这样的:重复应用代理调用方法

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) 
    { 
    //Grab a reference to the UISplitViewController 
    UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController; 

    PatientDetailViewController* patientDetailViewController = [splitViewController.viewControllers lastObject]; 
    splitViewController.delegate = patientDetailViewController; 


    //Grab a reference to the masterviewcontroller and get the first patient in the list. 
    UINavigationController *patientNavController = [splitViewController.viewControllers objectAtIndex:0]; 
    PatientMasterTableViewController *patientMasterViewController = (PatientMasterTableViewController *)[patientNavController topViewController]; 

    Patient* firstPatient = [[patientMasterViewController patientArray] objectAtIndex:0]; 

    //Set it as the detailviews patient. 
    [patientDetailViewController setPatient:firstPatient]; 

    //Set the detail's as the left's delegate. 
    patientMasterViewController.delegate = patientDetailViewController; 
    } 

return YES; 
} 

,并设置对象的方法是这样的:

-(void)setPatient:(Patient *)patient 
{ 

if (![self.patient isEqual:patient]) 
    { 
    self.patient = patient; 

    //Update the UI to reflect the new patient selected from the list. 
    [self refreshUI]; 
    } 
} 

是我遇到的问题是,setPatient方法将被调用不停直到程序崩溃,我不知道为什么。任何人都可以对此有所了解吗?

+4

是的,这是一个经典的无限循环。 'self.patient = patient'与调用'[self setPatient:patient]'相同' – 2014-10-20 05:35:59

+1

您是否实施了isEqual:对于Patient类?如果没有,那个电话可能不会达到你的预期。 – 2014-10-20 06:57:41

+0

通过@jshier进行良好调用:虽然建议(由Apple)在初始化属性时使用instanceVariable,但上述代码只能被另外调用一次。除了向代码添加噪声之外,平等检查不太可能实现多少。 – 2014-10-20 08:55:50

回答

0

此:

-(void)setPatient:(Patient *)patient 
{ 
if (![self.patient isEqual:patient]) 
    { 
    self.patient = patient; 

    //Update the UI to reflect the new patient selected from the list. 
    [self refreshUI]; 
    } 
} 

应该是:

-(void)setPatient:(Patient *)patient 
{ 
    _patient = patient; 
    [self refreshUI]; 
} 
+2

不,他最好使用底层实例变量来设置值。 – 2014-10-20 05:50:58

+0

@nickfalk,是的,我同意。 – WMios 2014-10-20 05:51:33