我正在构建一个基于主视图控制器(CalViewController)的应用程序来做一些数学运算,然后有两个视图控制器(DrinksViewController 和 FoodViewController)。
我构建了 CalViewController 和 DrinksViewController 但是当我想复制 DrinksViewController 来构建 FoodViewController(具有不同数据的相同视图)时,出现了一个错误:“在 FoodViewController 类型的对象上找不到属性'bouffon'”。
而在我的 foodViewController.h 中定义的“bouffon”然后由 CalViewController 导入。它适用于“CaloriesDepensees”(来自 DrinksViewController 的数据),但不适用于“bouffon”。我检查了 DrinksViewController.h 和 .m 的每一行,它似乎与 FoodViewController.h 和 .m 完全相同(显然具有不同的数据)。
自定义类和情节提要 ID 也是此视图控制器的 FoodViewController(在情节提要中,身份检查器)。
非常感谢您的帮助!
皮埃尔
这里 CalViewController.h :
#import <UIKit/UIKit.h>
#import "CalAppDelegate.h"
#import "DrinksViewController.h"
#import "FoodViewController.h"
@interface CalViewController : UIViewController
{
double temps;
double distance;
double vitesse;
double poids;
NSString *caloriesText;
}
@property (weak, nonatomic) IBOutlet UITextField *saisieTemps;
@property (weak, nonatomic) IBOutlet UITextField *saisieDistance;
@property (weak, nonatomic) IBOutlet UITextField *saisiePoids;
@property (weak, nonatomic) IBOutlet UITextView *resultat;
@property (weak, nonatomic) IBOutlet UISwitch *saisieDefaut;
@property (weak, nonatomic) IBOutlet UILabel *poidsDefaut;
- (IBAction)tempsAction:(id)sender;
- (IBAction)distanceAction:(id)sender;
- (IBAction)defautAction:(id)sender;
- (IBAction)saisieReturn :(id)sender;
- (IBAction)calcul:(id)sender;
- (IBAction)paramSwitch:(id)sender;
- (IBAction)poidsAction:(id)sender;
@end
这里 CalViewController.m :
@interface CalViewController ()
@end
@implementation CalViewController
@synthesize resultat;
@synthesize saisieDistance;
@synthesize saisieTemps;
@synthesize saisiePoids;
@synthesize saisieDefaut;
@synthesize poidsDefaut;
double calories;
double factCal;
double vitesse;
double poidsDonne=75;
- (void)viewDidLoad
{
[super viewDidLoad];
calories=1.0;
if (saisieDefaut.on) {
saisiePoids.hidden=YES;
poidsDefaut.hidden=NO;
}
else {
saisiePoids.hidden=NO;
poidsDefaut.hidden=YES;
}
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)tempsAction:(id)sender {
temps = [[saisieTemps text] doubleValue];
}
- (IBAction)distanceAction:(id)sender {
distance = [[saisieDistance text] doubleValue];
}
- (IBAction)poidsAction:(id)sender {
poids = [[saisiePoids text] doubleValue];
}
- (IBAction)defautAction:(id)sender {
}
- (IBAction)saisieReturn :(id)sender{
[sender resignFirstResponder];
}
- (IBAction)calcul:(id)sender {
temps = [[saisieTemps text] doubleValue];
distance = [[saisieDistance text] doubleValue];
vitesse = distance*1000/temps;
if (vitesse <110)
factCal=1.06;
else if (vitesse < 120)
factCal=1.052;
else if (vitesse < 130)
factCal=1.046;
else if (vitesse < 140)
factCal=1.041;
else if (vitesse < 150)
factCal=1.037;
else if (vitesse < 160)
factCal=1.034;
else if (vitesse < 280)
factCal=1.030;
else if (vitesse < 300)
factCal=1.035;
else if (vitesse < 320)
factCal=1.039;
else if (vitesse < 330)
factCal=1.043;
else if (vitesse < 340)
factCal=1.046;
else if (vitesse < 350)
factCal=1.049;
else if (vitesse < 360)
factCal=1.052;
else if (vitesse < 370)
factCal=1.055;
else if (vitesse < 380)
factCal=1.058;
else if (vitesse < 390)
factCal=1.049;
else if (vitesse>=390)
factCal=1.065;
if (saisieDefaut.on)
calories= factCal*distance*poidsDonne;
else
calories= factCal*distance*poids;
resultat.text = [NSString stringWithFormat:@"%0.f", calories];
}
- (IBAction)paramSwitch:(id)sender {
if (saisieDefaut.on) {
saisiePoids.hidden=YES;
poidsDefaut.hidden=NO;
}
else {
saisiePoids.hidden=NO;
poidsDefaut.hidden=YES;
}
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showFoodSegue"]){
FoodViewController *controller = (FoodViewController *)segue.destinationViewController;
controller.bouffon = calories;
错误出现在这里,'bouffon' 不是 FoodViewController 的对象。
}
if([segue.identifier isEqualToString:@"showDrinksSegue"]){
DrinksViewController *controllera = (DrinksViewController *)segue.destinationViewController;
controllera.caloriesDepensees = calories;
}
}
@end
这里 FoodViewController.h :
#import <UIKit/UIKit.h>
#import "CalAppDelegate.h"
@interface FoodViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *foodCollectionView;
@property(nonatomic) double bouffon;
@end
这里 FoodViewController.m :
#import "FoodViewController.h"
#import "FoodCell.h"
@interface FoodViewController ()
{
NSArray *arrayOfImages;
NSArray *arrayOfDescriptions;
NSArray *arrayOfCalories;
double caloriesFoodDouble;
NSString *caloriesFoodObject;
double receptionDouble;
}
@end
@implementation FoodViewController
@synthesize bouffon;
@synthesize foodCollectionView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[self foodCollectionView]setDataSource:self];
[[self foodCollectionView]setDelegate:self];
arrayOfImages = [[NSArray alloc]initWithObjects:@"beer2.jpg",@"biere1.png",nil];
arrayOfDescriptions =[[NSArray alloc]initWithObjects:@"bouffe",@"binouffe",nil];
arrayOfCalories =[[NSArray alloc]initWithObjects:@"1000",@"10000",nil];
}
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{ return [arrayOfDescriptions count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier1=@"Cell1";
FoodCell *cell1 = ([collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier1 forIndexPath:indexPath]);
[[cell1 foodImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
caloriesFoodObject=[arrayOfCalories objectAtIndex:indexPath.item];
caloriesFoodDouble = [caloriesFoodObject doubleValue];
caloriesFoodDouble=caloriesFoodDouble/bouffon;
[[cell1 foodLabel]setText:[NSString stringWithFormat:@"%.1f%@", caloriesFoodDouble,[arrayOfDescriptions objectAtIndex:indexPath.item]]];
return cell1;
}
- (NSInteger)numberOfSections:(UICollectionView *) collectionView
{return 1;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end