2010-07-26 10 views
0

J'ai un UIViewController (appelé AdjustViewController) qui présente une autre UIViewController (appelé SourcePickerViewController) avec un UIPickerView modalement. Je génère des instances de l'AdjustViewController et ils à leur tour font un . Je fais un NSDictionary et lui attribuer et un integer au AdjustViewController et à son tour, fixe les mêmes propriétés dans le SourcePickerController. De cette façon, je peux réutiliser les contrôleurs. Le NSDictionary est configuré dans un UITableViewController qui contient tous les AdjustViewController s.changements de propriété, mais je ne peux pas comprendre qui le fait

Le problème vient quand certains des préparateurs devraient avoir une composante et certains devraient avoir 2. L'entier que je passe le long est appelé numberOfComponents Quand je fais un sélecteur avec numberOfComponents = 1 en quelque sorte il change de = 2, mais je ne peux pas voir comment . J'ai NSLogs partout et je peux le voir se produire dès que la méthode numberOfComponentsInPickerView de délégué de sélecteur est appelée. C'est 1 juste avant et 2 juste après.

Il y a évidemment plus de code, mais je pense avoir toutes les parties importantes. Bien que si c'était vrai, peut-être que je saurais où est le problème!


intérieur 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]; 

intérieur 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]; 
} 

intérieur 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

Si tout le reste échoue, vous pouvez créer une classe debug-sous-écrit qui sur les propriétés méthode set ... qui imprime une pile trace/point d'arrêt avant d'appeler la classe super. Cela vous permettra de savoir d'où vient le changement au moins. – Akusete

Répondre

1

Je n'ai pas regardé à travers tout votre code; Au lieu de cela, je recommanderais d'écrire les propriétés de numberOfComponents au lieu de @ les synthétiser. Il suffit de se débarrasser de votre @synthesize et faire:

- (int)numberOfComponents { 
    return m_numberOfComponents; 
} 

et

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

Ensuite, définissez un point d'arrêt dans votre fonction setNumberOfComponents, et vous devriez être en mesure de voir quand il se fait appeler, vous peut voir ce qui se passe. J'espère que ça aide!

+0

Merci - Je les ai implémentés comme ceci: '- (int) numberOfComponents { \t return numberOfComponents; } 'et ' - (void) setNumberOfComponents: (entier) aNumberOfComponents { \t numberOfComponents = aNumberOfComponents; } ' Et je peux le voir s'appeler et l'appelant envoie un '1' et il semble s'appelé deux fois avant que le sélecteur est présenté. Première fois '- [AdjustViewController getChemicalSource:]' et deuxième fois '- [SourcePickerViewContronller viewDidLoad]'. – Steve

+0

Si vous survolez l'expression dans getChemicalSource, c'est la ligne suivante: 'sourcePickerViewController.numberOfComponents = self.numberOfComponents;' avec le côté gauche montrant '0' et le côté droit montrant' 1'. La deuxième fois dans viewDidLoad, c'est cette ligne: \t 'if (self.numberOfComponents = 2) {' et il affiche une valeur de '1' mais juste après cela, mon' NSLogs' montre qu'il est à nouveau '2'. – Steve

+0

Ah - votre commentaire m'avait attraper. (self.numberOfComponents = 2) devrait être (self.numberOfComponents == 2). Erreur gênante, n'est-ce pas? – Perrako