为什么这个 NSMutableArray 没有被填充?
我有一个 NSMutableArray 我正在尝试使用 for 循环进行填充并为其创建 NSStrings 。这些将成为我的 UIPickerView
的数据源。您会看到一些注释掉的行,其中我手动创建了 NSArrays
并且它们显示得很好,但我的 for-loop
NSMutableArray
似乎没有接受我正在制作的字符串。 NSLogs
显示我正在制作字符串(以及等效的 float
),但是 NSLogs
从 中提取值NSMutableArray
显示为 null
和 0.0
接口...
// 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
以及实现...
// 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在某处初始化数组?我在任何地方都没有看到
[[NSMutableArray alloc] init]
。向 null 对象发送消息是无操作,因此这些 addObjects 很可能被发送到 null 并且没有执行任何操作。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.