Note that there are some explanatory texts on larger screens.

plurals
  1. POUISwitch inside custom UITableViewCell not available
    primarykey
    data
    text
    <p>I have a <code>UISwitch</code> inside a custom <code>UITableViewCell</code> (the subclass of which I call <code>RootLabeledSwitchTableCell</code>).</p> <p>The cell contains a <code>UILabel</code> and <code>UISwitch</code> next to each other.</p> <p>I have a <code>@property</code> called <code>keychainSwitch</code> that points to the switch inside this custom cell:</p> <pre><code>@interface RootLabeledSwitchTableCell : UITableViewCell { IBOutlet UILabel *textLabel; IBOutlet UISwitch *labeledSwitch; } @property (nonatomic, retain) IBOutlet UILabel *textLabel; @property (nonatomic, retain) IBOutlet UISwitch *labeledSwitch; @end </code></pre> <p>In my table view delegate, I have added a selector that is called if the switch state is flipped:</p> <pre><code>- (UITableViewCell *) tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = [NSString stringWithFormat: @"%d:%d", [indexPath indexAtPosition:0], [indexPath indexAtPosition:1]]; UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { switch (indexPath.section) { case(kMyFirstSection): { switch (indexPath.row) { case(kMyFirstSectionFirstRow) { [cellOwner loadMyNibFile:@"RootLabeledSwitchTableCell"]; cell = (RootLabeledSwitchTableCell *)cellOwner.cell; self.keychainSwitch = [(RootLabeledSwitchTableCell *)cell labeledSwitch]; [self.keychainSwitch addTarget:self action:@selector(keychainOptionSwitched) forControlEvents:UIControlEventValueChanged]; break; } // ... } } // ... } } </code></pre> <p>So this selector works correctly:</p> <pre><code>- (void) keychainOptionSwitched { NSLog(@"Switched keychain option from %d to %d", ![self.keychainSwitch isOn], [self.keychainSwitch isOn]); } </code></pre> <p>However, I cannot use the <code>UISwitch</code> instance's <code>-setOn:animated:</code> method to initialize its initial state:</p> <pre><code>- (void) updateInterfaceState { BOOL isFirstTimeRun = [[[NSUserDefaults standardUserDefaults] objectForKey:kIsFirstTimeRunKey] boolValue]; if (isFirstTimeRun) { [self.keychainSwitch setOn:NO animated:NO]; } } </code></pre> <p>From testing, <code>self.keychainSwitch</code> is <code>nil</code> which is causing <code>-setOn:animated</code> to do nothing, but I can still operate the switch and the <code>NSLog</code> statement will correctly print the switch state changes, e.g.:</p> <pre><code>[Session started at 2009-08-24 07:04:56 -0700.] 2009-08-24 07:04:58.489 MyApp[29868:20b] keychain switch is: nil 2009-08-24 07:05:00.641 MyApp[29868:20b] Switched keychain option from 1 to 0 2009-08-24 07:05:01.536 MyApp[29868:20b] Switched keychain option from 0 to 1 2009-08-24 07:05:02.928 MyApp[29868:20b] Switched keychain option from 1 to 0 </code></pre> <p>Is there something I am missing about setting <code>self.keychainSwitch</code> in the <code>UITableView</code> delegate method?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload