我主要有以下代码header file
NSMutableArray *array;
NSDictionary *dict,*dict1;
.m
文件包含显示项目名称及其相应价格的字典数组,我已将其显示在UITableView
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text=[NSString stringWithFormat:@"%@ %@",[[array objectAtIndex:indexPath.row]objectForKey:@"name"],[[array objectAtIndex:indexPath.row]objectForKey:@"price"]];
return cell;
}
当用户选择任何一个项目时,它会将dictionary
另一个dictionary
称为通行证发送到VegQuantityViewController
数量必须乘以价格的地方
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==0)
{
VegQuantity *vegetarian1 = [[VegQuantity alloc] initWithNibName:@"VegQuantity" bundle:nil];
vegetarian1.pass=dict;
[self presentModalViewController:vegetarian1 animated:YES];
}
if(indexPath.row==1)
{
VegQuantity *vegetarian1 = [[VegQuantity alloc] initWithNibName:@"VegQuantity" bundle:nil];
vegetarian1.pass=dict1;
[self presentModalViewController:vegetarian1 animated:YES];
}
}
- (void)viewDidLoad
{
array=[NSMutableArray array];
dict=[NSDictionary dictionaryWithObjectsAndKeys:@"20.00",@"price",@"tomato soup",@"name",nil];
[array addObject:dict];
dict1=[NSDictionary dictionaryWithObjectsAndKeys:@"10.00",@"price",@"Veg Manchow soup",@"name",nil];
[array addObject:dict1];
NSLog(@"The array of dictionaries contains%@",array);
[super viewDidLoad];
}
VegQuantity.h
NSDictionary *pass;
IBOutlet UIButton *done;
IBOutlet UITextField *input,*output;
NSNumber *prices;
float x,y,c;
VegQuantity.m
该done
按钮只是检索该特定菜肴的密钥并将其与用户输入的数量相乘,并将其textfield
显示在textfield
我NSInvalidArgumentException
在通过objectForKey
线上面临的输出中,这可能是什么问题?
-(IBAction)done:(id)sender
{
prices=[pass objectForKey:@"price"];
x=[input.text floatValue];
y=[prices floatValue];
c=x*y;
output.text=[NSString stringWithFormat:@"%f",c];
}