I have created a custom checkbox control. I am using this in a view where preferences are being set. The view loads the preferences and loops through them to determine the type of control to display based on the preference definition.
For each preference, there is a Monitor and an Alert checkbox created. I can create the checkboxes, but not all of them work. Initially, the first, third, fifth etc work, but the demo data I am using requires about 30 checkboxes to be created.
Eventually - after 20 or so, the subsequent ones no longer work at all. They are set properly based on the code, but cant be changed. I have stared at this for a day now and cant see why it should not work. Any help is greatly appreciated.
Code for custom checkbox:
@implementation CustomCheckBox{
@private NSString *theLabelText;
}
@synthesize isChecked;
+ (CustomCheckBox*)newCheckbox {
return [[CustomCheckBox alloc] initWithFrame:CGRectMake(0.0f,0.0f,25.0f,25.0f)];
}
+ (CustomCheckBox*)newCheckbox: (double)xValue yValue:(double)yValue widthValue: (double)widthValue heightValue: (double) heightValue {
return [[CustomCheckBox alloc] initWithFrame:CGRectMake(xValue,yValue,widthValue,heightValue)];
}
- (id)initWithFrame:(CGRect)frame {
if (self=[super initWithFrame:frame]) {
super.backgroundColor = [UIColor clearColor];
[self setIsChecked: NO];
[self setImage:[UIImage imageNamed:@"checkbox_not_ticked"] forState:UIControlStateNormal];
[self addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];
return self;
}
return nil;
}
- (void)setChecked:(BOOL)value {
if (self.isChecked==value)
return;
self->isChecked = value;
if (self.isChecked){
[self setImage:[UIImage imageNamed:@"checkbox_ticked"] forState:UIControlStateNormal];
}else{
[self setImage:[UIImage imageNamed:@"checkbox_not_ticked"] forState:UIControlStateNormal];
}
[self setNeedsDisplay];
}
- (void)clicked {
if (isChecked) {
self.isChecked = NO;
[self setImage:[UIImage imageNamed:@"checkbox_not_ticked"] forState:UIControlStateNormal];
} else {
self.isChecked = YES;
[self setImage:[UIImage imageNamed:@"checkbox_ticked"] forState:UIControlStateNormal];
}
[self setNeedsDisplay];
}
Basic simple class. Now I am using this class in a code loop like so:
int iFound = -1;
for (NSString* lbl in oneRule.FieldNames) {
iFound +=1;
CustomCheckBox *chk;
if ([[lbl uppercaseString] isEqualToString:@"MONITOR"] ){
UILabel *Label2 = [self CreateLabelWithPrompt: lbl Xval:25.0 Yval:controlY+3 fontSize:12.0];
//Add label to cell
[vRuleEdit addSubview: Label2];
chk = [[CustomCheckBox alloc] initWithFrame: CGRectMake(75, controlY, 25, 25)];
if ([[oneRule.FieldValues[iFound] uppercaseString] isEqualToString:@"TRUE"]){
[chk setChecked: true];
}else{
[chk setChecked: false];
}
[vRuleEdit addSubview: chk];
}else if ([[lbl uppercaseString] isEqualToString:@"ALERT"]){
UILabel *Label2 = [self CreateLabelWithPrompt: lbl Xval:150.0 Yval:controlY+3 fontSize:12.0];
//Add label to cell
[vRuleEdit addSubview: Label2];
chk = [[CustomCheckBox alloc] initWithFrame: CGRectMake(200, controlY, 25, 25)];
if ([[oneRule.FieldValues[iFound] uppercaseString] isEqualToString:@"TRUE"]){
[chk setChecked: true];
}else{
[chk setChecked: false];
}
[vRuleEdit addSubview: chk];
}//end if
}//for (NSString* lbl in oneRule.FieldNames)
It is being done like this because the monitor and alert checkboxes have to show up first if there are more parameters to a preference. Anyhow, this is what the view looks like after painting. I can click on the Monitor checkbox and change its state. If I add a breakpoint to the clicked method of this class it gets fired. But the Alert checkbox never works, and like I said, further down the view they all stop working...
Aucun commentaire:
Enregistrer un commentaire