I have a label that serves as a warning when certain conditions are not met. But when I attempt to set the label to hidden with the following code:
infoWarning.hidden = YES;
for context, here is the full code:
if (weightC < 50 || (waistM1 < 20 && waistM2 < 20 && waistM3 < 20) || (ageC < 18 ))
{
infoWarning.hidden = NO;
NSString *warning = @"";
if(ageC < 18)
{
warning = @" an age over 17";
}
if (averageWaistM < 20) {
if (warning != @"") {
warning = [NSString stringWithFormat:@"%@, and", warning];
}
warning = [NSString stringWithFormat:@"%@ a waist measurement", warning];
}
if (weightC < 50) {
if (warning != @"") {
warning = [NSString stringWithFormat:@"%@, and", warning];
}
warning = [NSString stringWithFormat:@"%@ a weight", warning];
}
if (warning!=@"") {
[infoWarning setText:[NSString stringWithFormat:@"Please add%@.", warning]] ;
}
} else {
if(gender.selectedSegmentIndex == 0)
{
}
else if(gender.selectedSegmentIndex == 1)
{
}
}
That I thought would work, but (obviously) does not. It gives me an EXC_BAD_ACCESS error on the line:
infoWarning.hidden = YES;
infoWarning does exist, is synthesized, and is connected to the UILabel in Interface Builder. Could you help me identify the problem and make it work?
This is the .h file:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@class Preferences;
@class Utilities;
@interface BodyFatCalculatorViewController : UIViewController {
IBOutlet UITextField *age;
IBOutlet UITextField *waist1;
IBOutlet UITextField *waist2;
IBOutlet UITextField *waist3;
IBOutlet UITextField *weight;
IBOutlet UILabel *waist;
IBOutlet UILabel *currentWeight;
IBOutlet UILabel *bodyFat;
IBOutlet UILabel *averageWaist;
IBOutlet UILabel *veryLean;
IBOutlet UILabel *veryLeanBorder;
IBOutlet UIImageView *veryLeanCheckmark;
IBOutlet UILabel *fit;
IBOutlet UILabel *fitBorder;
IBOutlet UIImageView *fitheckmark;
IBOutlet UILabel *average;
IBOutlet UILabel *averageBorder;
IBOutlet UIImageView *averageCheckmark;
IBOutlet UILabel *atRisk;
IBOutlet UILabel *atRiskBorder;
IBOutlet UILabel *atModRisk;
IBOutlet UILabel *atModRiskBorder;
IBOutlet UILabel *atHighRisk;
IBOutlet UILabel *atHighRiskBorder;
IBOutlet UILabel *infoWarning;
IBOutlet UISegmentedControl *gender;
NSMutableArray *arrayAge;
}
@property (nonatomic, retain) IBOutlet UITextField *age;
@property (nonatomic, retain) IBOutlet UITextField *waist1;
@property (nonatomic, retain) IBOutlet UITextField *waist2;
@property (nonatomic, retain) IBOutlet UITextField *waist3;
@property (nonatomic, retain) IBOutlet UITextField *weight;
@property (nonatomic, retain) IBOutlet UILabel *waist;
@property (nonatomic, retain) IBOutlet UILabel *currentWeight;
@property (nonatomic, retain) IBOutlet UILabel *bodyFat;
@property (nonatomic, retain) IBOutlet UILabel *averageWaist;
@property (nonatomic, retain) IBOutlet UILabel *veryLean;
@property (nonatomic, retain) IBOutlet UILabel *veryLeanBorder;
@property (nonatomic, retain) IBOutlet UIImageView *veryLeanCheckmark;
@property (nonatomic, retain) IBOutlet UILabel *fit;
@property (nonatomic, retain) IBOutlet UILabel *fitBorder;
@property (nonatomic, retain) IBOutlet UIImageView *fitCheckmark;
@property (nonatomic, retain) IBOutlet UILabel *average;
@property (nonatomic, retain) IBOutlet UILabel *averageBorder;
@property (nonatomic, retain) IBOutlet UIImageView *averageCheckmark;
@property (nonatomic, retain) IBOutlet UILabel *atRisk;
@property (nonatomic, retain) IBOutlet UILabel *atRiskBorder;
@property (nonatomic, retain) IBOutlet UILabel *atModRisk;
@property (nonatomic, retain) IBOutlet UILabel *atModRiskBorder;
@property (nonatomic, retain) IBOutlet UILabel *atHighRisk;
@property (nonatomic, retain) IBOutlet UILabel *atHighRiskBorder;
@property (nonatomic, retain) IBOutlet UILabel *infoWarning;
@property (nonatomic, retain) IBOutlet UISegmentedControl *gender;
-(IBAction)valueChanged:(id)sender;
-(IBAction)textFieldDoneEditing:(id)sender;
-(void)calculate;
@end
Problem has been fixed, thank you to those who responded and looked into it. The problem was actually an array with the limits that were too small (I don’t know why that caused this to break, they weren’t at all related…) and so I increased the limits and now the infoWarning works great.
The complete error from the console might help but given “EXC_BAD_ACCESS error” in the line
suggests that there is a problem with
infoWarning.hidden, what are the@propertyand@synthesizestatements?To compare the contents of strings use:
not:
which compares the pointers to the strings.
But in your case why not just use: