这个适当的代码是否适用于具有两个相同组件的选择器中的数组?或者两个组件可以以某种方式使用相同的数组吗?
- (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];
另外,如何使用来自组件和文本字段的值正确格式化计算?这不起作用。
我尝试了这个,但是使用'row'我得到了未声明的标识符错误。
- (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
答案 0 :(得分:0)
好的,有一些事情:
首先,你的数组正在泄漏内存,因为你alloc
,init
它们没有释放它们(除非你使用ARC,在这种情况下这很好)。
其次,如果您希望它们共享同一个数组,为什么不执行类似
的操作 self.toArray = self.fromArray;
self.toValues = self.fromValues;
如何使用来自组件和文本字段的值正确格式化计算?
你需要在这里更清楚,我不明白你想要做什么。
然而,在你的第一个代码块中:
在您尝试使用row
时,它未定义。您可能需要了解variables and scope。
为什么不在班级_row
@interface
NSInteger _row;
并将该变量设置为
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
_row = row;
}
然后,当你需要时使用_row
?
至于最后一块代码,你想用[fromValues text]
做些什么?