我正在尝试制作一个包含2列选择器的计算应用程序。选择器的左侧是数据值,数据值是从具有每个项目价格的plist中提取的。拾取器的另一侧是从1到64的字符串。
我正在试图找出如何获取选择器的两个部分的字符串值并将它们转换为int(因此它们可以相乘并打印出来)
这是我的代码(完整版)
#pragma mark - View lifecycle
- (void)viewDidLoad
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath =[bundle pathForResource:@"worthList" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.dataValues = dictionary;
[dictionary release];
NSArray *components = [self.dataValues allKeys];
NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)];
self.values = sorted;
NSMutableArray *number =[[NSMutableArray alloc] init];
for(int x = 1; x < 65; x++)
{
NSString * numberString = [[NSString alloc] initWithFormat:@"%d",x];
[number addObject:numberString];
}
self.quantity = number;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
self.doublePicker = nil;
self.dataValues = nil;
self.values = nil;
self.quantity = nil;
[self setDoublePicker:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)calculateItem:(id)sender
{
NSString *message = [[NSString alloc] initWithFormat:@""];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Thank you" message:message delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil];
[alert show];
[alert release];
[message release];
}
#pragma mark -
#pragma Picker data source methods
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
-(NSInteger)pickerView:(UIPickerView *) pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == kValueComponent)
{
return [self.values count];
}
return [self.quantity count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component
{
if (component == kValueComponent)
{
return [self.values objectAtIndex:row];
}
return [self.quantity objectAtIndex:row];
}
@end
答案 0 :(得分:1)
尝试将数据字符串转换为int,然后进行计算:
int = [yourString intValue];
答案 1 :(得分:0)
试试这个:
NSString *str=[NSString stringWithFormat:@"%@",[pickerArr objectAtIndex:row]];
int test=[str intValue];
答案 2 :(得分:0)
这里的分钟和秒数是阵列。
选择器的委托方法。 您也可以使用其他委托方法。
秒和分钟是在你的案例使用数组中加载选择器的数组,该数组是用plist填充的。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch (component) {
case 0:
minuteTime =[minutes objectAtIndex:row] intValue];
break;
case 1:
secondTime = [[seconds objectAtIndex:row] intValue];
break;
}
}
您还可以使用此方法进一步区分字符串。
NSArray *greenTime = [yourstring componentsSeparatedByString:@":"];
NSLog(@"Green Time : %@\nFirst : %@\nSecond : %@",greenTime,[greenTime objectAtIndex:0],[greenTime objectAtIndex:1]);