我正在尝试编写一个 iPhone/iPad 屏幕,该屏幕根据在文本字段中输入的内容和在 2 组件选择器中选择的内容输出值。我已经完成了基础知识,直到出现输入/选择的数据,但没有执行计算。我已经搜索了我能找到的所有资源,并使用了我找到的所有相关代码,如下所示。
具体来说,我想允许用户在一个文本字段中输入美元金额,选择支付频率(每小时、每月等),选择他们想要将其转换为的支付频率,然后在当他们点击按钮时的其他文本字段。另一个按钮将清除字段,因此他们可以转换另一个金额。更具体地说,如果他们想查看每周 310 美元的半月工资是多少,他们将在第一个字段中输入“310”,在第一个选择器组件中选择“每周”,在另一个组件中选择“半月”,然后点击计算按钮。然后,另一个字段将显示 $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
对于冗长的帖子和格式不正确的代码,我深表歉意。我希望有人能告诉我哪些代码是不必要的以及完成此任务需要什么。