如何使用双组件Picker和Text字段条目在Xcode中编码计算

时间:2012-07-18 16:35:45

标签: xcode picker

我正在尝试对iPhone / iPad屏幕进行编码,该屏幕根据在文本字段中输入的内容以及在双组分选择器中选择的内容输出值。我已完成基本操作,直到输入/选择的数据出现,但没有执行计算。我搜索了我能找到的所有资源,并使用了我找到的所有相关代码,如下所示。

具体来说,我想让用户在一个文本字段中输入一个美元金额,选择付费频率(每小时,每月等),选择他们希望将其转换为的付费频率,并查看点击按钮时会在其他文本字段中生成结果。另一个按钮会清除字段,因此它们可以转换另一个数量。更具体地说,如果他们想看看他们每周310美元的工资是半月,他们会在第一个字段中输入“310”,在第一个选择器组件中选择“Weekly”,在另一个组件中选择“SemiMonthly”,然后点击计算按钮。然后另一个领域将显示$ 671.67(310 * 52/24 = 671.67)。

万一它是相关的,它发生的视图控制器被绑定到一个导航控制器作为连接到主视图控制器的6个屏幕之一的5个屏幕之一(它需要几个点击才能到达这个屏幕)。

到目前为止,我可以在两个文本字段中输入数字,然后我可以旋转2个选择器组件并使其选择显示在标签中,但这就是我被卡住的地方。我找不到如何实现它的例子。

这是我的ViewController.h代码:

    @interface PayFrequencyViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>
{
    UIPickerView *payPicker;
    NSArray *fromArray;
    NSArray *toArray;
    NSArray *fromValues;
    NSArray *toValues;
    UILabel *resultText;
    UITextField *inputText;
}
@property (strong, nonatomic) IBOutlet UIPickerView *payPicker;
@property (strong, nonatomic) NSArray *fromArray;
@property (strong, nonatomic) NSArray *toArray;
@property (strong, nonatomic) NSArray *fromValues;
@property (strong, nonatomic) NSArray *toValues;
@property (strong, nonatomic) IBOutlet UILabel *resultText;
@property (strong, nonatomic) IBOutlet UITextField *inputText;
- (IBAction)Convert:(id)sender;
- (IBAction)Clear:(id)sender;
@end

这是我的ViewController.m代码:

    @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 = [[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];


    [super viewDidLoad];
    inputText.keyboardType = UIKeyboardTypeDecimalPad;
    // Do any additional setup after loading the view.
}

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

//- (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]];
}

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

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

@end

我对冗长的帖子和格式不正确的代码表示歉意。我希望有人能告诉我哪些代码是不必要的,以及完成这项任务需要什么。

0 个答案:

没有答案