I am having some trouble while working with UISwitch on a static UITableView
.
I have to restore the last state of a certain UISwitch
when the App loads, but whenever I check the state of the IBOutlet
, it is nil. I have tried to manually alloc the variable, which was of no help either.
Here is what I am doing:
SettingsController.h
//IBOutlet connected correctly
@property (strong, nonatomic) IBOutlet UISwitch *switch_displayDetail;
SettingsController.m
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (!_switch_displayDetail) {
NSLog(@"_switch_displayDetail is NIL"); //This is always displayed
}
//Switch is default to YES, I am trying to set it to NO
//This line does nothing...
[_switch_displayDetail setOn:[dataManager shouldDisplayDetail] animated:YES];
}
Everywhere else when I check the state of _switch_displayDetail
it is nil. I am calling all the super initialization methods.
Did anything change on iOS 6.1?
[EDIT] Using the synthesized variable does not work either.
[EDIT 2] Found the problem to be a bug on Xcode or on iPhone Simulator. After testing on my iPod touch, the initial algorithm worked perfectly.
I am going nuts with this problem...
I know first hand how frustrating those Twilight zone issues can be. There's obviously too little information to diagnose but I thought I'd share how I'd approach the debugging.
self
viewWillAppear
, log the same thingI have a feeling we're not talking about the same instance here. If I'm right, the setter will display a value, but not viewWillAppear
. If I'm wrong, then both will be nil and in that case either you're not calling the right init
method, or your linkage isn't right in IB.
Just my two cents.
Thanks everyone for your answers, but after almost giving up, I tested my App on my iPod touch and it worked perfectly. This looks like a bug on the iPhone Simulator or, much probably a bug on Xcode (nothing new here). After reseting all settings and data on the iPhone Simulator menu, it worked there too.
So, before going crazy, test your App on a real device.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With