views:

464

answers:

1

I have two UIPickerViews set up on one view under the same view controller. The picker's seem to be populated with values as expected. I also have a label for each picker. The label updates with each value change, but the value displayed on the label is not correct. It's always off by 10. The codes are below.

Interface:

#import <UIKit/UIKit.h>
#import "InfoViewController.h"



@interface TransactionsViewController : UIViewController {

    //Deposit
    NSMutableArray *depositArray;
    IBOutlet UIPickerView *depositPicker;
    IBOutlet UILabel *depositLabel;

    //Send
    NSMutableArray *sendArray;
    IBOutlet UIPickerView *sendPicker;
    IBOutlet UILabel *sendLabel;
}

@property (nonatomic, retain) NSMutableArray *depositArray;
@property (nonatomic, retain) IBOutlet UIPickerView *depositPicker;
@property (nonatomic, retain) NSMutableArray *sendArray;
@property (nonatomic, retain) IBOutlet UIPickerView *sendPicker;
@property (nonatomic, retain) IBOutlet UILabel *depositLabel;
@property (nonatomic, retain) IBOutlet UILabel *sendLabel;

@end

Implementation:

#import "TransactionsViewController.h"


@implementation TransactionsViewController
@synthesize depositArray;
@synthesize depositPicker;
@synthesize sendArray;
@synthesize sendPicker;
@synthesize depositLabel;
@synthesize sendLabel;


}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];


    NSMutableArray *dollarsArray = [[NSMutableArray alloc] init];
    for (int i = 5; i <= 100; i+=5)
    {
        NSString *item = [[NSString alloc] initWithFormat:@"%i", i];
        [dollarsArray addObject:item];

        [item release];
    }

    self.depositArray = dollarsArray;
    self.sendArray = dollarsArray;

    [dollarsArray release]; 


}

// Pickers
#pragma mark - Pickers
#pragma mark Pickers Data Source Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(pickerView == depositPicker) {
    switch(component) {
        case 0:
            return 1;
            break;
        case 1:
            return [depositArray count];
            break;
        default:
            break;
    }
    }
    else if(pickerView == sendPicker) {
        switch(component) {
            case 0:
                return 1;
                break;
            case 1:
                return [sendArray count];
                break;
            default:
                break;
        }
    }
    return 0;
}

-(CGFloat)pickerView:(UIPickerView*)pickerView widthForComponent:(NSInteger)component {
    switch (component) {
        case 0:
            return 50;
            break;
        case 1:
            return 100;
            break;
        default:
            break;
    }
    return 0;
}

#pragma mark Pickers Delegate Methods

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(pickerView == depositPicker) {
    switch(component) {
        case 0:
            return @"$";
            break;
        case 1:
            depositLabel.text=[depositArray objectAtIndex:row];
            return [depositArray objectAtIndex:row];
            [depositLabel release];
            break;
        default:
            break;
    }
    }
    else if(pickerView == sendPicker) {
        switch(component) {
            case 0:
                return @"$";
                break;
            case 1:
                return [sendArray objectAtIndex:row];
                break;
            default:
                break;
        }
    }
    return 0;
}

//Pickers End


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

}


- (void)dealloc {
    [depositArray release];
    [send release];
    [super dealloc];

}


@end

Everything, such as the sendLabel, is not set up yet. As you can see, the depositLabel automatically updates with the selected row, however, the value is not updating to the correct value. I'm not sure if it's my implementation of the label or the picker itself that is the problem.

I'd appreciate some inputs. Thanks.

+1  A: 

The titleForRow method is just where you're supposed to return the text for the row. In your titleForRow, it doesn't make sense to set the label text there (nor do I think should you release it). The "return 0" at the end of titleForRow may also cause problems.

To handle an actual select in a picker, use the didSelectRow method.

DyingCactus
Thanks a lot. It worked. :)
intl