2014-03-01 6 views
5

CoreData 관계의 문제를 탐색하는 간단한 프로그램이라고 생각했던 것을 만들었습니다.iOS 내 UIPickerview 메소드가 반복되는 이유는 무엇입니까?

그러나이 두 가지 방법은 예기치 않게 segue 후에 루프되고 (대상에 성공적으로 도착할 때 VC). 참여 방법은 numberOfRowsInComponent 있습니다 (4 번 루프)와 titleForRow 전에이 NSLog 출력을 생성, (5 번 루프) 계속 : 난

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    frc = [WMDGCategory MR_fetchAllGroupedBy:nil withPredicate:nil sortedBy:@"name" ascending:YES]; 
    NSInteger *catCount; 
    catCount = frc.fetchedObjects.count; 
    NSLog(@"Number of categories is %zd", catCount); 
    return frc.fetchedObjects.count; 
} 

#pragma mark Picker View delegate 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; 
{ 
    NSString * description; 
    description = [[frc.fetchedObjects objectAtIndex:row]description]; 
    NSLog(@"Description of row is %@", description); 
    return [[frc.fetchedObjects objectAtIndex:row]description]; 
} 

:

2014-03-01 12:12:26.231 EntitiesAndRelationships[64785:a0b] Number of categories is 2 
2014-03-01 12:12:26.233 EntitiesAndRelationships[64785:a0b] Number of categories is 2 
2014-03-01 12:12:26.236 EntitiesAndRelationships[64785:a0b] Number of categories is 2 
2014-03-01 12:12:26.237 EntitiesAndRelationships[64785:a0b] Number of categories is 2 
2014-03-01 12:12:26.239 EntitiesAndRelationships[64785:a0b] Description of row is <WMDGCategory: 0x8a8a420> (entity: WMDGCategory; id: 0x8a8e2c0 <x-coredata://75EA0FB2-AB3F-480A-9EBE-CDA14EDEB902/WMDGCategory/p2> ; data: <fault>) 
2014-03-01 12:12:26.242 EntitiesAndRelationships[64785:a0b] Description of row is <WMDGCategory: 0x8a8c110> (entity: WMDGCategory; id: 0x8a8e2d0 <x-coredata://75EA0FB2-AB3F-480A-9EBE-CDA14EDEB902/WMDGCategory/p1> ; data: <fault>) 
2014-03-01 12:12:26.246 EntitiesAndRelationships[64785:a0b] Description of row is <WMDGCategory: 0x8a8a420> (entity: WMDGCategory; id: 0x8a8e2c0 <x-coredata://75EA0FB2-AB3F-480A-9EBE-CDA14EDEB902/WMDGCategory/p2> ; data: <fault>) 
2014-03-01 12:12:26.247 EntitiesAndRelationships[64785:a0b] Description of row is <WMDGCategory: 0x8a8c110> (entity: WMDGCategory; id: 0x8a8e2d0 <x-coredata://75EA0FB2-AB3F-480A-9EBE-CDA14EDEB902/WMDGCategory/p1> ; data: <fault>) 
2014-03-01 12:12:26.249 EntitiesAndRelationships[64785:a0b] Description of row is <WMDGCategory: 0x8a8a420> (entity: WMDGCategory; id: 0x8a8e2c0 <x-coredata://75EA0FB2-AB3F-480A-9EBE-CDA14EDEB902/WMDGCategory/p2> ; data: <fault>) 

다음 두 가지 방법에 대한 코드입니다 꽤 당황 스럽네. 누군가가 도와 줄 수 있니?

감사합니다. viewDidLoad

frc = [WMDGCategory MR_fetchAllGroupedBy:nil withPredicate:nil sortedBy:@"name" ascending:YES]; 

하지만 루프가 계속 :

아래 reecon의 대답은 당

편집 나는이 줄을 움직였다.

답변

4

데이터 소스 및 대리자 메서드는 여러 번 호출 될 수 있습니다. 이는 문제가되지 않습니다.

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component; 

또는

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; 

언제든지 여러 번 호출 할 수 있습니다

당신은 방법이 있다고 가정한다. 따라서 구현은 가볍습니다. 다른 곳으로 가져 오는 코드를 이동하십시오. ~ viewDidLoad. 따라서 한 번만 가져옵니다. 그런 다음 선택기가 대리인에게 정보를 요청하면 가져 오지 않은 결과를 가져 오지 않습니다.

+0

감사! 위의 편집을 참조하십시오. – rattletrap99

+0

데이터 소스 및 대리자 메서드가 여러 번 호출 될 수 있습니다. 이는 문제가되지 않습니다. 귀하의 코드가 올바르게 작동합니까? – RaffAl

+0

예, 지금까지는 아주 좋습니다. 나는 아직도 구현할 물건이 있지만, 나는 그 작은 변덕에 매달렸다. 나를 똑바로 세워 줘서 고마워,하지만 나 한테서 다시 소식을 듣고 놀라지 마라. :) – rattletrap99

관련 문제