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;
}
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