2014-10-20 73 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