财产发生变化,但我不知道是谁做的

发布于 2024-09-11 01:08:02 字数 7165 浏览 8 评论 0原文

我有一个 UIViewController (称为 AdjustViewController),它呈现另一个带有 UIPickerView 的 UIViewController (称为 SourcePickerViewController)模态。我生成 AdjustViewController 的实例,它们又创建一个 SourcePickerViewController。我创建一个 NSDictionary 并将其和一个 integer 分配给 AdjustViewController,然后它又在 SourcePickerController 中设置相同的属性代码>.这样我就可以重用控制器。 NSDictionary 在一个 UITableViewController 中设置,其中包含所有 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;
}

I have a UIViewController (called AdjustViewController) that presents another UIViewController (called SourcePickerViewController) with a UIPickerView modally. I generate instances of the AdjustViewController and they in turn make a SourcePickerViewController. I make an NSDictionary and assign it and an integer to the AdjustViewController and it in turn sets the same properties in the SourcePickerController. This way I can reuse the controllers. The NSDictionary get set up in a UITableViewController that has all the AdjustViewControllers in it.

The problem comes when some of the pickers should have 1 component and some should have 2. The integer that I pass along is called numberOfComponents When I make a picker with numberOfComponents = 1 somehow it's changing to = 2 but I can't see how. I have NSLogs all over the place and I can see it happen as soon as the picker delegate method numberOfComponentsInPickerView is called. It's 1 right before and 2 right after.

There's obviously more code, but I think I have all the important parts. Although if that were true, maybe I'd know where the problem is!


Inside 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];

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身软味 2024-09-18 01:08:05

我没有查看你所有的代码;相反,我建议写出 numberOfComponents 的属性,而不是 @synthesize'ing 它们。只需摆脱 @synthesize,然后 make:

 - (int)numberOfComponents {
   return m_numberOfComponents;
}

然后

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

,在 setNumberOfComponents 函数中设置一个断点,您应该能够看到它何时被调用,这样您就可以看到发生了什么。我希望这有帮助!

I didn't look through all of your code; Instead, I'd recommend writing out the properties for numberOfComponents instead of @synthesize'ing them. Just get rid of your @synthesize, and make:

 - (int)numberOfComponents {
   return m_numberOfComponents;
}

and

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

Then, set a breakpoint in your setNumberOfComponents function, and you should be able to see whenever it's getting called, so you can see what is going on. I hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文