views:

36

answers:

1

I have an NSMutableArray I am trying to populate using a for-loop and making NSStrings for it. These are to be the data source for my UIPickerView. You'll see some commented out lines where I manually made NSArrays and they showed up fine, but my for-loop NSMutableArray doesn't seem to accept the strings I'm making. The NSLogs show that I am making the string (and an equivalent float) alright, but the NSLogs that pull the values from the NSMutableArray show up as null and 0.0

The interface...


//  PoolSizePickerViewController.h

#import <UIKit/UIKit.h>
#define kLengthComponent 0
#define kWidthComponent 1
#define kDepthComponent 2

@protocol PoolSizePickerViewControllerDelegate;

@interface PoolSizePickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
    UIPickerView *poolSizePicker;
    float length, width, depth;
    NSMutableArray *lengthStrings, *widthStrings, *depthStrings;
    NSMutableArray *lengthFloats, *widthFloats, *depthFloats;
    NSString *pickerType;
    NSString *pickerDescription;
    UIButton *selectButton;
    UIButton *cancelButton;
    UILabel *pickerTitleLabel;
    UITextView *pickerDescriptionLabel;
    id <PoolSizePickerViewControllerDelegate> delegate;
}

@property (nonatomic, retain) IBOutlet UIPickerView *poolSizePicker;
@property float length, width, depth;
@property (nonatomic, retain) NSMutableArray *lengthStrings, *widthStrings, *depthStrings;
@property (nonatomic, retain) NSMutableArray *lengthFloats, *widthFloats, *depthFloats;
@property (nonatomic, retain) NSString *pickerType;
@property (nonatomic, retain) NSString *pickerDescription;
@property (nonatomic, retain) IBOutlet UIButton *selectButton;
@property (nonatomic, retain) IBOutlet UIButton *cancelButton; 
@property (nonatomic, retain) IBOutlet UILabel *pickerTitleLabel;
@property (nonatomic, retain) IBOutlet UITextView *pickerDescriptionLabel;
@property (assign) id <PoolSizePickerViewControllerDelegate> delegate;

- (IBAction)selectedSelectButton;
- (IBAction)selectedCancelButton;

@end

@protocol PoolSizePickerViewControllerDelegate <NSObject>

@optional

- (void)poolSizePickerViewController:(PoolSizePickerViewController *)controller 
                 didSelectLength:(float)length
                        andWidth:(float)width 
                        andDepth:(float)depth;

- (void)poolSizePickerViewController:(PoolSizePickerViewController *)controller 
               didSelectCancel:(BOOL)didCancel;

@end

And the implementation...


//  PoolSizePickerViewController.m

#import "PoolSizePickerViewController.h"

@implementation PoolSizePickerViewController

@synthesize poolSizePicker;
@synthesize length, width, depth;
@synthesize lengthStrings, widthStrings, depthStrings;
@synthesize lengthFloats, widthFloats, depthFloats;
@synthesize delegate;
@synthesize pickerType, pickerDescription;
@synthesize selectButton, cancelButton;
@synthesize pickerTitleLabel;
@synthesize pickerDescriptionLabel;

- (IBAction)selectedSelectButton {
    NSInteger lengthRow = [poolSizePicker selectedRowInComponent:kLengthComponent];
    NSInteger widthRow = [poolSizePicker selectedRowInComponent:kWidthComponent];
    NSInteger depthRow = [poolSizePicker selectedRowInComponent:kDepthComponent];
    length = [[self.lengthFloats objectAtIndex:lengthRow] floatValue];
    width = [[self.widthFloats objectAtIndex:widthRow] floatValue];
    depth = [[self.depthFloats objectAtIndex:depthRow] floatValue];
    if ([self.delegate respondsToSelector:@selector (poolSizePickerViewController:didSelectLength:andWidth:andDepth:)]) {
        [self.delegate poolSizePickerViewController:self didSelectLength:length andWidth:width andDepth:depth];
    }
}

- (IBAction)selectedCancelButton {
    if ([self.delegate respondsToSelector:@selector (poolSizePickerViewController:didSelectCancel:)]) {
        [self.delegate poolSizePickerViewController:self didSelectCancel:YES];
    }
}


- (void)viewDidLoad {
    for (int footIndex = 6; footIndex < 40; footIndex ++) {
        for (int inchIndex = 0; inchIndex < 2; inchIndex ++) {
            [self.lengthStrings addObject:[NSString stringWithFormat:@"   %d' %d\"", footIndex, inchIndex * 6]];
            NSLog(@"   %d' %d\"", footIndex, inchIndex * 6);
            NSLog(@"   -%@", [self.lengthStrings objectAtIndex:footIndex - 6]);
            [self.lengthFloats addObject:[NSNumber numberWithFloat:(float)footIndex + (float)inchIndex * 0.5f]];
            NSLog(@"   %1.1f", (float)footIndex + (float)inchIndex * 0.5f);
            NSLog(@"   -%1.1f", [[self.lengthFloats objectAtIndex:footIndex - 6] floatValue]);
        }
    }
    for (int footIndex = 6; footIndex < 40; footIndex ++) {
    for (int inchIndex = 0; inchIndex < 2; inchIndex ++) {
            [self.widthStrings addObject:[NSString stringWithFormat:@"   %d' %d\"", footIndex, inchIndex * 6]];
            [self.widthFloats addObject:[NSNumber numberWithFloat:(float)footIndex + (float)inchIndex * 0.5f]];
        }
    }
    for (int footIndex = 1; footIndex < 16; footIndex ++) {
        for (int inchIndex = 0; inchIndex < 2; inchIndex ++) {
            [self.depthStrings addObject:[NSString stringWithFormat:@"   %d' %d\"", footIndex, inchIndex * 6]];
            [self.depthFloats addObject:[NSNumber numberWithFloat:(float)footIndex + (float)inchIndex * 0.5f]];
        }
    }

//  lengthStrings = [NSArray arrayWithObjects:@"   6' 0\"", nil];
//  lengthFloats = [NSArray arrayWithObjects:[NSNumber numberWithFloat:1.0], nil];
//  widthStrings = [NSArray arrayWithObjects:@"   6' 0\"", nil];
//  widthFloats = [NSArray arrayWithObjects:[NSNumber numberWithFloat:2.0], nil];
//  depthStrings = [NSArray arrayWithObjects:@"   1' 0\"", nil];
//  depthFloats = [NSArray arrayWithObjects:[NSNumber numberWithFloat:3.0], nil];

    UIImage *buttonImageNormal = [UIImage imageNamed:@"whiteButton.png"];
    UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [selectButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
    [cancelButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
    UIImage *buttonImagePressed = [UIImage imageNamed:@"blueButton.png"];
    UIImage *stretchableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [selectButton setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
    [cancelButton setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
    pickerTitleLabel.text = [NSString stringWithFormat:@"Select a Pool %@", pickerType];
    pickerDescriptionLabel.text = self.pickerDescription;
    self.poolSizePicker.showsSelectionIndicator = YES;
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (void)dealloc {
    [super dealloc];
}

   #pragma mark -
   #pragma mark Picker Data Source Methods

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    switch (component) {
        case kLengthComponent:
            return [self.lengthStrings count];
            break;
        case kWidthComponent:
            return [self.widthStrings count];
            break;
        default:
            return [self.lengthStrings count];
            break;
    }
}

#pragma mark Picker Delegate Methods

- (NSString *)pickerView:(UIPickerView *)pickerView
         titleForRow:(NSInteger)row
        forComponent:(NSInteger)component {
    switch (component) {
        case kLengthComponent:
            return [self.lengthStrings objectAtIndex:row];
            break;
        case kWidthComponent:
            return [self.widthStrings objectAtIndex:row];
            break;
        default:
            return [self.depthStrings objectAtIndex:row];
            break;
    }
}

@end
+2  A: 

Are you initializing your arrays somewhere? I don't see an [[NSMutableArray alloc] init] anywhere. Sending a message to a null object is a no-op, so it's likely that those addObjects are being sent to a null and aren't doing anything.

samkass
That makes sense. I've conditioned myself to `alloc` as little as possible! But how come it worked with the manual arrays? Is there something different about `[NSMutableArray addObject:]` as opposed to `[NSArray arrayWithObjects:]`? And doesn't the `@property (nonatomic, retain)` initialize them? I'll try `alloc` and `init` in `viewDidLoad` and then release them in `dealloc`...
Steve
@Steve, [NSArray arrayWithObjects:] is equal to [[[NSArray alloc] initWithObjects:] autorelease].
vfn
Boy I wish there was a reference of what get autoreleased! I spend lots of time in the Apple documentation, but I don't mention of autorelease status there. Thanks!
Steve
Most things are autoreleased unless the method names includes init, new, or copy.
Daniel Wood
there IS a reference of what is autoreleased, check: http://developer.apple.com/mac/library/documentation/cocoa/conceptual/MemoryMgmt/MemoryMgmt.html
Leg10n