2010-07-26 71 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