0

对于具有两个相同组件的选择器中的数组,这是合适的代码吗?或者两个组件可以以某种方式使用相同的数组?

    - (void)viewDidLoad
{
    self.fromArray = [[NSArray alloc] initWithObjects:
                      @"Annual", @"Monthly", @"48/Year", @"SemiMonthly", @"BiWeekly", @"Weekly", @"Hourly", nil];

    self.fromValues = [[NSArray alloc] initWithObjects: 
                       [NSNumber numberWithFloat:1.0],
                       [NSNumber numberWithFloat:12.0], 
                       [NSNumber numberWithFloat:48.0],
                       [NSNumber numberWithFloat:24.0], 
                       [NSNumber numberWithFloat:26.0],
                       [NSNumber numberWithFloat:52.0], 
                       [NSNumber numberWithFloat:2080.0], nil];


    self.toArray = [[NSArray alloc] initWithObjects:
                      @"Annual", @"Monthly", @"48/Year", @"SemiMonthly", @"BiWeekly", @"Weekly", @"Hourly", nil];

    self.toValues = [[NSArray alloc] initWithObjects: 
                     [NSNumber numberWithFloat:1.0],
                     [NSNumber numberWithFloat:12.0], 
                     [NSNumber numberWithFloat:48.0],
                     [NSNumber numberWithFloat:24.0], 
                     [NSNumber numberWithFloat:26.0],
                     [NSNumber numberWithFloat:52.0], 
                     [NSNumber numberWithFloat:2080.0], nil];

另外,如何使用来自组件和文本字段的值正确格式化计算?这只是行不通。

我试过这个,但使用“行”时出现未声明的标识符错误。

- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component
{
    if (component == 0) {
        return [fromArray objectAtIndex:row];
    }
    return [toArray objectAtIndex:row];
} 
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component {
}

- (IBAction)Convert:(id)sender {
    float valuein = [[fromValues objectAtIndex:row] floatValue];
    float valueout = [[toValues objectAtIndex:row] floatValue];
    float input = [inputText.text floatValue];
    float result = input * valuein / valueout;
    NSString *resultString = [[NSString alloc] initWithFormat: @"$ %@", [toArray objectAtIndex:row]];
    resultText.text = [NSString stringWithFormat:@"$%6.2f",result];
}

编辑:这是完整 .m 文件代码的当前版本,如下所示。我怀疑有一些不必要的代码。唯一的警告是未使用的变量“resultString”。

#import "PayFrequencyViewController.h"

@interface PayFrequencyViewController ()

@end

@implementation PayFrequencyViewController
@synthesize payPicker;
@synthesize toArray;
@synthesize fromArray;
@synthesize toValues;
@synthesize fromValues;
@synthesize resultText;
@synthesize inputText;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.fromArray = [[NSArray alloc] initWithObjects:
                      @"Annual", @"Monthly", @"48/Year", @"SemiMonthly", @"BiWeekly", @"Weekly", @"Hourly", nil];

    self.fromValues = [[NSArray alloc] initWithObjects: 
                       [NSNumber numberWithFloat:1.0],
                       [NSNumber numberWithFloat:12.0], 
                       [NSNumber numberWithFloat:48.0],
                       [NSNumber numberWithFloat:24.0], 
                       [NSNumber numberWithFloat:26.0],
                       [NSNumber numberWithFloat:52.0], 
                       [NSNumber numberWithFloat:2080.0], nil];

    self.toArray = self.fromArray;
    self.toValues =  self.fromValues;

    [super viewDidLoad];
    inputText.keyboardType = UIKeyboardTypeDecimalPad;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.toArray = nil;
    self.fromArray = nil;
    self.resultText = nil;
    self.inputText = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

    if (component == 0) {
        return [fromArray count];
    }
    return [toArray count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component
{
    if (component == 0) {
        return [fromArray objectAtIndex:row];
    }
    return [toArray objectAtIndex:row];
} 

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component {
    _row = row;
}

- (IBAction)Convert:(id)sender {
//    float valuein = [[fromValues text] floatValue];
//    float valueout = [[toValues text] floatValue];
//    [resultText setText:[NSString stringWithFormat:@"%6.2f", result]];
//    [inputText resignFirstResponder]; 
    float valuein = [[fromValues objectAtIndex:_row] floatValue];
    float valueout = [[toValues objectAtIndex:_row] floatValue];
    float input = [inputText.text floatValue];
    float result = input * valuein / valueout;
    NSString *resultString = [[NSString alloc] initWithFormat: @"$ %@", [toArray objectAtIndex:_row]];
    resultText.text = [NSString stringWithFormat:@"$%6.2f",result];
}

- (IBAction)Clear:(id)sender {
    inputText.text = @"";
    resultText.text = @"";
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [inputText resignFirstResponder];
}
@end
4

1 回答 1

0

好的,有几件事:

首先,您的数组正在泄漏内存,因为您alloc没有init释放它们(除非您使用 ARC,在这种情况下这很好)。

其次,如果你想让他们共享同一个数组,你为什么不做类似的事情

 self.toArray = self.fromArray;
 self.toValues =  self.fromValues;

?

如何使用来自组件和文本字段的值正确格式化计算?

你需要在这里更清楚,我不明白你在做什么。

但是,在您的第一段代码中:在您尝试使用row它时没有定义。您可能需要了解变量和范围

为什么不在你_row的类中创建一个实例变量@interface

NSInteger _row;

并将该变量设置为

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
  inComponent:(NSInteger)component {
      _row = row;
 }

ant 那么,_row在需要的时候再使用?

至于最后一段代码,你想用什么来实现[fromValues text]

于 2012-07-30T16:02:56.773 回答