2010-07-26 72 views
0

我有一個UIViewController(稱爲AdjustViewController)另一個UIViewController(稱爲SourcePickerViewController)與UIPickerView呈現模態。我生成了AdjustViewController的實例,並且他們又創建了一個SourcePickerViewController。我製作了一個NSDictionary,並將它和一個integer分配給AdjustViewController,然後在SourcePickerController中設置相同的屬性。這樣我可以重新使用控制器。 NSDictionaryUITableViewController中設置,其中包含所有AdjustViewController屬性的變化,但我無法弄清楚誰在做它

問題是當一些拾荒者應該有1個組件和一些應有2.我一起傳遞整數稱爲numberOfComponents當我做與numberOfComponents = 1揀貨機不知何故它更改爲= 2,但我看不出。我在各地都有NSLogs,只要調用代理方法numberOfComponentsInPickerView,就可以看到它發生。之前是1,之後是2

顯然有更多的代碼,但我認爲我有所有重要的部分。雖然如果那是真的,也許我會知道問題出在哪裏!


MenuViewController.m

- (void)viewDidLoad { 
    NSLog(@"ChemicalViewController launched"); 
    self.title = @"Adjust Chemicals"; 
    NSMutableArray *array = [[NSMutableArray alloc] init]; 

// Chlorine Controller 
    AdjustViewController *chlorineAdjustViewController = [[AdjustViewController alloc] initWithNibName:@"AdjustViewController" bundle:nil]; 
    chlorineAdjustViewController.title = @"FC - Free Chlorine"; 
    chlorineAdjustViewController.numberOfComponents = 2; 
    NSLog(@"Generating chlorine source dictionary"); 
    NSDictionary *chlorineSourceDictionary = [self generateChlorineDictionary]; 
    chlorineAdjustViewController.dictionaryOfSources = chlorineSourceDictionary; 
    [chlorineSourceDictionary release]; 
    [array addObject:chlorineAdjustViewController]; 
    [chlorineAdjustViewController release]; 

// CYA Controller 
    AdjustViewController *cyaAdjustViewController = [[AdjustViewController alloc] initWithNibName:@"AdjustViewController" bundle:nil]; 
    cyaAdjustViewController.title = @"CYA - Cyanuric Acid"; 
    cyaAdjustViewController.numberOfComponents = 1; 
    NSLog(@"Generating cya source dictionary"); 
    NSDictionary *cyaSourceDictionary = [self generateCYADictionary]; 
    cyaAdjustViewController.dictionaryOfSources = cyaSourceDictionary; 
    [cyaSourceDictionary release]; 
    [array addObject:cyaAdjustViewController]; 
    [cyaAdjustViewController release]; 

AdjustViewController.m

// Present the picker for chlorine selection 
- (IBAction)getChemicalSource { 
    SourcePickerViewController *sourcePickerViewController = [[SourcePickerViewController alloc] init]; 
    sourcePickerViewController.delegate = self; 
    NSLog(@"getChemicalSource setting numberOfComponents %d", self.numberOfComponents); 
    sourcePickerViewController.numberOfComponents = self.numberOfComponents; 
    NSLog(@"getChemicalSource sending numberOfComponents %d", sourcePickerViewController.numberOfComponents); 
    sourcePickerViewController.dictionaryOfSources = self.dictionaryOfSources; 
    [self presentModalViewController:sourcePickerViewController animated:YES]; 
    [sourcePickerViewController release]; 
} 

#pragma mark - 
#pragma mark Picker View Delegate Methods 

// Returns the values from the picker if a source was chosen 
- (void)sourcePickerViewController:(SourcePickerViewController *)controller 
       didSelectSource:(NSString *)source 
       andConcentration:(NSString *)concentration 
        andConstant:(float)constant 
        andIsLiquid:(BOOL)isLiquid { 


    sourceField.text = [[NSString alloc] initWithFormat:@"%@, %@", source, concentration]; 
    [self updateAdvice]; 
    NSLog(@"Returned source = %@, concentration = %@, sourceConstant = %1.7f, isLiquid = %d", source, concentration, constant, isLiquid); 
    [self dismissModalViewControllerAnimated:YES]; 
} 

// Returns from the picker without choosing a new source 
- (void)sourcePickerViewController:(SourcePickerViewController *)controller 
       didSelectCancel:(BOOL)didCancel { 
    [self updateAdvice]; 
    NSLog(@"Returned without selecting source"); 
    [self dismissModalViewControllerAnimated:YES]; 
} 

SourceViewController.m

- (void)viewDidLoad { 
    NSLog(@"SourcePickerViewController launched"); 
    NSLog(@"viewDidLoad"); 
    NSLog(@"Received numberOfComponents %d", self.numberOfComponents); 
    self.chemicalSources = dictionaryOfSources; 
    NSArray *components = [self.chemicalSources allKeys]; 
    NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)]; 
    self.sources = sorted; // This array has the chemical sources 

    if (self.numberOfComponents = 2) { 
     NSString *selectedSource = [self.sources objectAtIndex:0]; 
     NSArray *chemArray = [self.chemicalSources objectForKey:selectedSource]; 
     NSMutableArray *concentrationArray = [[NSMutableArray alloc] init]; 
     int num = [chemArray count]; 
     for (int i=0; i<num; i++) { 
      [concentrationArray addObject:[[chemArray objectAtIndex:i] chemConcentration]]; 
     } 
     self.concentrations = concentrationArray; 
    } 
    [super viewDidLoad]; 
} 

    #pragma mark - 
    #pragma mark Picker Data Source Methods 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 
    NSLog(@"numberOfComponentsInPickerView, self.numberOfComponents = %d", self.numberOfComponents); 
    return self.numberOfComponents; 
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 
    NSLog(@"numberOfRowsInComponent, self.numberOfComponents = %d", self.numberOfComponents); 
    if (component == kSourceComponent) 
     return [self.sources count]; 
    return [self.concentrations count]; 
} 

#pragma mark Picker Delegate Methods 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 
    if (component == kSourceComponent) 
     return [self.sources objectAtIndex:row]; 
    return [self.concentrations objectAtIndex:row]; 
} 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 
    NSLog(@"didSelectRow, self.numberOfComponents = %d", self.numberOfComponents); 
    if (numberOfComponents = 2) { 
     if (component == kSourceComponent) { 
      NSString *selectedSource = [self.sources objectAtIndex:row]; 
      NSArray *chemArray = [self.chemicalSources objectForKey:selectedSource]; 
      NSMutableArray *concentrationArray = [[NSMutableArray alloc] init]; 
      int num = [chemArray count]; 
      for (int i=0; i<num; i++) { 
       [concentrationArray addObject:[[chemArray objectAtIndex:i] chemConcentration]]; 
      } 
    self.concentrations = concentrationArray; 
    [picker selectRow:0 inComponent:kConcentrationComponent animated:YES]; 
    [picker reloadComponent:kConcentrationComponent]; 
    } 
} 
} 

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { 
    if (component == kConcentrationComponent) 
     return 90; 
    return 205; 
} 
+0

如果一切都失敗了,你可以創建一個調試子類,它覆蓋了屬性設置方法...在調用超類之前打印一個堆棧跟蹤/斷點。這會讓你知道變化至少來自哪裏。 – Akusete 2010-07-26 02:18:45

回答

1

我沒看完所有的代碼;相反,我建議寫出numberOfComponents的屬性而不是@合成它們。剛剛擺脫你的@synthesize,並使:

- (int)numberOfComponents { 
    return m_numberOfComponents; 
} 

- (void)setNumberOfComponents(int aNumberOfComponents) { 
    m_numberOfComponents = aNumberOfComponents; 
} 

然後,設置在setNumberOfComponents功能斷點,你應該能夠看到,每當它獲取調用,所以你可以看到發生了什麼。我希望這有助於!

+0

謝謝 - 我實現它們是這樣的: ' - (int)numberOfComponents { \t return numberOfComponents; }和 ' - (void)setNumberOfComponents:(int)aNumberOfComponents { \t numberOfComponents = aNumberOfComponents; }' 而且我可以看到它被調用,調用者發送它爲'1',它似乎在提交選擇器之前調用兩次。 第一次' - [AdjustViewController getChemicalSource:]'和第二次' - [SourcePickerViewContronller viewDidLoad]'。 – Steve 2010-07-26 03:29:53

+0

如果您將鼠標懸停在getChemicalSource中的表達式上,則此行爲:'sourcePickerViewController.numberOfComponents = self.numberOfComponents;',左側顯示爲'0',右側顯示爲'1'。 第二次在viewDidLoad中,它是這一行:\t'if(self.numberOfComponents = 2){'它顯示值爲'1',但在那之後,我的'NSLogs'再次顯示它是'2'。 – Steve 2010-07-26 03:33:04

+0

啊 - 你的評論讓我抓住它。 (self.numberOfComponents = 2)應該是(self.numberOfComponents == 2)。惱人的錯誤,不是嗎? – Perrako 2010-07-26 03:53:50