i can not find what is not working with two buttons i create. the buttons will work as intended if there is data in my NSString
plantingEventData.seedingMethod
(if it contains “conventional” or “VR”). but if any other value is in the variable, the button does nothing. the method is still called, and will get into the if statements in the button methods, but will not change the properties. its almost like the buttons are not connected the UIButton. but it works perfect if the variable has “conventional” or “VR” strings in it.
Good
plantingEventData.seedingMethod = @"conventional" or @"VR"
Bad
plantingEventData.seedingMetod = any other value
i create two buttons that will are set up for either one or neither to be selected:
UIButton *conventionalSeedingButton = [UIButton new];
conventionalSeedingButton.frame = CGRectMake(200, ((i * 40) + 143), 20, 20);
conventionalSeedingButton.tag = 1;
[conventionalSeedingButton addTarget:self action:@selector(conventionalSeedingMethodBtn:) forControlEvents:UIControlEventTouchUpInside];
if ([plantingEventData.seedingMethod isEqualToString:@"conventional"])
{
NSLog(@"convention button is set to true");
[conventionalSeedingButton setImage:[UIImage imageNamed:@"radio-btn-selected.png"] forState:UIControlStateNormal];
conventionalSeedingButton.selected = TRUE;
}
else
{
NSLog(@"convention button is set to false");
[conventionalSeedingButton setImage:[UIImage imageNamed:@"radio-btn.png"] forState:UIControlStateNormal];
conventionalSeedingButton.selected = FALSE;
}
UIButton *VRSeedingButton = [UIButton new];
VRSeedingButton.frame = CGRectMake(200, ((i * 40) + 143), 20, 20);
VRSeedingButton.tag = 2;
[VRSeedingButton addTarget:self action:@selector(VRSeedingMethodBtn:) forControlEvents:UIControlEventTouchUpInside];
[VRSeedingButton setImage:[UIImage imageNamed:@"radio-btn.png"] forState:UIControlStateNormal];
if ([plantingEventData.seedingMethod isEqualToString:@"VR"])
{
NSLog(@"VR button is set to true");
[VRSeedingButton setImage:[UIImage imageNamed:@"radio-btn-selected.png"] forState:UIControlStateNormal];
VRSeedingButton.selected = TRUE;
}
else
{
NSLog(@"VR button is set to false");
[VRSeedingButton setImage:[UIImage imageNamed:@"radio-btn.png"] forState:UIControlStateNormal];
VRSeedingButton.selected = FALSE;
}
the methods the buttons call:
-(IBAction)conventionalSeedingMethodBtn:(id)sender
{
NSLog(@"self.conventionalSeedingMethodButton.selected: %@", self.conventionalSeedingMethodButton.selected ? @"YES" : @"NO");
if (self.conventionalSeedingMethodButton.selected)
{
NSLog(@"switching from true to false");
[self.conventionalSeedingMethodButton setImage:[UIImage imageNamed:@"radio-btn.png"] forState:UIControlStateNormal];
self.conventionalSeedingMethodButton.selected = FALSE;
}
else
{
NSLog(@"switching from false to true");
[self.conventionalSeedingMethodButton setImage:[UIImage imageNamed:@"radio-btn-selected.png"] forState:UIControlStateNormal];
self.conventionalSeedingMethodButton.selected = TRUE;
}
[self.VRSeedingMethodButton setImage:[UIImage imageNamed:@"radio-btn.png"] forState:UIControlStateNormal];
self.VRSeedingMethodButton.selected = FALSE;
}
-(IBAction)VRSeedingMethodBtn:(id)sender
{
NSLog(@"self.VRSeedingMethodButton.selected: %@", self.VRSeedingMethodButton.selected ? @"YES" : @"NO");
if (self.VRSeedingMethodButton.selected)
{
NSLog(@"switching from true to false");
[self.VRSeedingMethodButton setImage:[UIImage imageNamed:@"radio-btn.png"] forState:UIControlStateNormal];
self.VRSeedingMethodButton.selected = FALSE;
}
else
{
NSLog(@"switching from false to true");
[self.VRSeedingMethodButton setImage:[UIImage imageNamed:@"radio-btn-selected.png"] forState:UIControlStateNormal];
self.VRSeedingMethodButton.selected = TRUE;
}
[self.conventionalSeedingMethodButton setImage:[UIImage imageNamed:@"radio-btn.png"] forState:UIControlStateNormal];
conventionalSeedingMethodButton.selected = FALSE;
}
i’m baffled =?
Your code is a bit confusing. Looks like you have a mix of stuff defined in Interface Builder along with stuff you’ve created programmatically.
If your intent is to use IB:
viewDidLoadif you want to change/set any properties.If your intent is to create/configure the buttons programmatically:
Your buttons are not added to a view. This means that whatever buttons you are seeing in your app must have been added somewhere else (likely IB).
You are not assigning the buttons to iVars. This is ok but you are referring to iVars in your event handlers. If no iVars, retrieve the buttons via their tag properties.