Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On iOS 6.1, UISwitch IBOutlet is always nil

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...

like image 935
Bruno Philipe Avatar asked Feb 04 '13 13:02

Bruno Philipe


2 Answers

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.

  1. Override the setter for switch_displayDetail, in there, log the value but also self
  2. Then in viewWillAppear, log the same thing

I 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.

like image 68
mprivat Avatar answered Nov 15 '22 04:11

mprivat


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.

like image 30
Bruno Philipe Avatar answered Nov 15 '22 06:11

Bruno Philipe