I am trying to implement a custom implementation of switching buttons so that only one can be selected at a time. I have run into a weird error where I init an object using [[ajdSwitchButton alloc] init]. In the init I set a class property as follows self.currentSelection = 2.
The issue is that between the init and the first call to an IBAction method, the value is changed to 0. I cannot figure out why. Here is the relevant code:
ajdSwitchButton.h
#import <UIKit/UIKit.h>
@interface ajdSwitchButton : UIView
@property (nonatomic) NSInteger currentSelection;
// Button Outlets
@property (nonatomic, strong) IBOutlet UIButton *buttonOne;
@property (nonatomic, strong) IBOutlet UIButton *buttonTwo;
// Button Actions
- (IBAction)buttonPress:(id)sender;
- (IBAction)buttonPressTwo:(id)sender;
// Instance Methods
- (void)switchButtonState:(UIButton *)button;
@end
ajdSwitchButton.m
#import "ajdSwitchButton.h"
@implementation ajdSwitchButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_currentSelection = 2;
}
return self;
}
// Handles button press actions for buttonOne
- (IBAction)buttonPress:(id)sender {
NSLog(@"%@", self);
// Y button pressed
if (self.currentSelection == 2) {
// No button is selected
// Highlight and select buttonOne
[self performSelector:@selector(switchButtonState:) withObject:self.buttonOne afterDelay:0.0];
} else if (self.currentSelection == 1) {
// No was previously selected
// Unselect NO and select YES
[self performSelector:@selector(switchButtonState:) withObject:self.buttonTwo afterDelay:0.0];
[self performSelector:@selector(switchButtonState:) withObject:self.buttonOne afterDelay:0.0];
} else {
// Y button already pressed
[self performSelector:@selector(switchButtonState:) withObject:self.buttonOne afterDelay:0.0];
}
self.currentSelection = 0;
}
- (IBAction)buttonPressTwo:(id)sender {
// N button pressed
NSLog(@"%i", self.currentSelection);
if (self.currentSelection == 2) {
// No button is selected
// Highlight and select buttonOne
[self performSelector:@selector(switchButtonState:) withObject:self.buttonTwo afterDelay:0.0];
} else if (self.currentSelection == 0) {
// Yes was previously selected
// Unselect YES and select NO
[self performSelector:@selector(switchButtonState:) withObject:self.buttonTwo afterDelay:0.0];
[self performSelector:@selector(switchButtonState:) withObject:self.buttonOne afterDelay:0.0];
} else {
// N button already pressed
[self performSelector:@selector(switchButtonState:) withObject:self.buttonTwo afterDelay:0.0];
}
self.currentSelection = 1;
}
// Switches the look and state of the button
- (void)switchButtonState:(UIButton *)button {
if (!button.selected) {
button.highlighted = YES;
button.selected = YES;
} else {
button.highlighted = NO;
button.selected = NO;
}
}
@end
I link an instance of ajdSwitchButton to an IBOutlet view within ViewController. Any help would be greatly appreciated.
EDIT: I pasted some NSLogs to check the memory value of the object directly after the init and as soon as the IBAction method is called. Here is the before and after:
<ajdSwitchButton: 0x746d2e0; frame = (0 0; 0 0); layer = <CALayer: 0x7472c40>>
<ajdSwitchButton: 0x7471b10; frame = (77 232; 180 83); autoresize = TM+BM; layer = <CALayer: 0x7471bf0>>
IBOutlets init through initWithCoder:(NSCoder)aDecoder, so you need to implement this method.