我正在制作一个卡路里计数器,并创建了一个 UIAlertView,它为我提供了食物清单。我做了一个NSMutableDictionary
包含食物和卡路里的:
@implementation FoodDatabase
-(id)init
{
self = [super init];
if(self)
{
food= [[NSMutableDictionary alloc] init];
[food setObject:@"111" forKey:@"Rice"];
}
return self;
}
-(NSString *) foodList: (NSString *) foodItem
{
for(NSString *key in [food allKeys])
{
if([foodItem isEqual: key])
{
NSLog(@"%@",foodItem);
return [food objectForKey:foodItem];
}
}
}
@end
在另一堂课中,我创建了一个UIAlertView
提供食物清单的列表。这是商品 Rice 的代码片段:
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if([buttonTitle isEqualToString:@"Rice"])
{
NSString *xyz = [foodData foodList: @"Rice"];
food_calorie = ([xyz floatValue]);
UIAlertView *rice_alert=[[UIAlertView alloc] initWithTitle:@"Enter quantity of rice consumed" message:@"100 gms = 111 calories" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[rice_alert addTextFieldWithValue:@"" label:@"Enter quantity in gms"];
RiceText = [rice_alert textFieldAtIndex:0];
RiceText.keyboardType = UIKeyboardTypeNumberPad;
RiceText.clearsOnBeginEditing = YES;
RiceText.clearButtonMode = UITextFieldViewModeWhileEditing;
RiceText.keyboardAppearance = UIKeyboardAppearanceAlert;
rice_alert.tag = RiceAlertView;
[rice_alert show];
[rice_alert release];
}
_RiceText_
我使用用户在 中输入的值和_object_
为特定键(在本例中为大米)返回的值来计算总卡路里。但它似乎没有返回 的值,_object_
因为它的值NSLog
显示_(null)_
为_xyz_
。我哪里错了??