我有一个标签,当某些条件不符合时会作为警告。但是当我尝试使用以下代码将标签设置为隐藏时:
infoWarning.hidden = YES;
对于上下文,这是完整的代码:
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)
{
}
}
我认为这样可行,但(显然)没有。它在行上给我一个EXC_BAD_ACCESS错误:
infoWarning.hidden = YES;
infoWarning确实存在,已合成,并且已连接到Interface Builder中的UILabel。你能帮我找出问题并使其有效吗?
这是.h文件:
#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
问题已得到解决,感谢那些做出回应并对其进行调查的人。问题实际上是一个限制太小的数组(我不知道为什么会导致这种情况破坏,它们根本没有相关...)所以我增加了限制,现在infoWarning工作得很好。
答案 0 :(得分:3)
来自控制台的完整错误可能有所帮助,但在行中出现“EXC_BAD_ACCESS错误”
infoWarning.hidden = NO;
表示infoWarning.hidden
存在问题,@property
和@synthesize
语句是什么?
比较字符串的内容使用:
[warning is equalToString:@""]
不
warning != @""
比较指向字符串的指针。
但在你的情况下,为什么不使用:
warning.length != 0