0

これは、2 つの同一コンポーネントを持つピッカー内の配列に適切なコードですか? それとも、両方のコンポーネントが何らかの方法で同じ配列を使用できますか?

    - (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