2016-04-10 38 views
0

環境:Xcode的7.2,構建版本7C68,Objective-C的NSInternalInconsistencyException在UICollectionViewController在Xcode 7.2/Objective-C的

在我的應用我有一個UIViewController,名爲GameView,包含UICollectionViewController,名爲「questionCollectionViewController 「,其中有一個名爲」gameQuestions「的NSMutableArray,因爲它是數據源。 GameView有一個「重置遊戲」按鈕,可以執行一個展開式遊戲。前展開SEGUE執行我可以看到UICollectionViewController位於0x7a79e810和的NSMutableArray位於0x7b936bd0

(lldb) print self.questionCollectionViewController 
(QuestionCollectionViewController *) $0 = 0x7a79e810 

(lldb) print self.questionCollectionViewController.gameQuestions 
(__NSArrayM *) $1 = 0x7b936bd0 @"38 objects" 

退繞SEGUE之前,我清除經由的NSMutableArray removeAllObjects的gameQuestions陣列()方法:

-(void) resetGameState { 
    [[self.questionCollectionViewController gameQuestions] removeAllObjects]; 
} 

開卷SEGUE後,當我重新演繹到的UIViewController我得到的UICollectionViewController的一個新實例,實例化SEGUE /填充新的NSMutableArray:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if([[segue identifier] isEqualToString:@"QuestionCollectionSegue"]) { 
     self.questionCollectionViewController = [segue destinationViewController]; 
     [self.questionCollectionViewController setGameQuestions:[[self game] copyGameQuestions]]; 
    } 
} 

在此之後我可以看到UICollectionViewController現在位於0x7a700e30和NSMutableArray裏現在是0x7a60a4a0

(lldb) print self.questionCollectionViewController 
(QuestionCollectionViewController *) $2 = 0x7a700e30 

(lldb) print self.questionCollectionViewController.gameQuestions 
(__NSArrayM *) $3 = 0x7a60a4a0 @"40 objects" 

當我選擇一個問題,通過。該didSelectItemAtIndexPath委託方法,didSelectItemAtIndexPath完成執行後,我能看到的CollectionView和NSMutableArray中仍處於新地址:

(lldb) print self 
(QuestionCollectionViewController *) $4 = 0x7a700e30 

(lldb) print self.gameQuestions 
(__NSArrayM *) $5 = 0x7a60a4a0 @"40 objects" 

由於地址都很好didSelectItemAtIndexPath完成後,我沒有看到一個問題didSelectItemAtIndexPath。但是,一旦我點擊這個問題的的UITextField,突然的和的CollectionView現在的NSMutableArray的非之前恢復到了在開卷SEGUE清除原始地址/對象,在0x7a79e8100x7b936bd0進行風賽格瑞和前的NSMutableArray被清除,並重新實例:

(lldb) print self 
(QuestionCollectionViewController *) $6 = 0x7a79e810 

(lldb) print self.gameQuestions 
(__NSArrayM *) $7 = 0x7b936bd0 @"0 objects" 

這會導致應用程序與NSInternalInconsistencyException崩潰,因爲顯示的CollectionView有40個對象,但現在它的數據源現在已經莫名其妙地被收歸到舊的清除NSMutableArray。

我在UIKeyboardDidShowNotification上訂閱了通知,並且地址已經在這一點上改變了。我UITextFieldDelegates樣子:

# pragma mark - UITextFieldDelegate 
-(BOOL) textFieldShouldReturn:(UITextField *)textField { 
    [textField resignFirstResponder]; 
    return TRUE; 
} 

- (void) textFieldDidBeginEditing:(UITextField *)textField { 
    [self enableSubmitButton:TRUE]; 
    [[self QVAnswerTextField] setPlaceholder: @""]; 
} 

- (void) textFieldDidEndEditing:(UITextField *)textField { 
    [self checkValidAnswer]; 
} 

沒有任何人有任何想法可能是導致UICollectionViewController要恢復到自己的一箇舊實例,它是某種方式仍然被保留在內存中?

回答

0

好的我想通了,這是一個愚蠢的錯誤,我會在這裏發佈解決方案,以防其他人遇到這個問題。在我的情況下,我訂閱了通知(通過defaultCenter),但我沒有明確註銷通知,所以它保留了舊的CollectionView對象的引用。雖然我不確定如何/爲什麼這些地址在視圖本身中混在一起,但當我在「重置遊戲」操作期間手動取消訂閱通知時,此問題不再發生。

相關問題