AddTarget: action: forControlEvents - UISwitch in TableView - sender ok, the event is always 0x0

Using the fantastic posts in this forum, I created the switch as an auxiliary element in the tableView. When the switch is switched, my action (switchChanged) is called. Only the sender has a valid value, the event is 0x0.

Adding a target to switchView:

        [switchView addTarget:self action:@selector(switchChanged:forEvent:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];

Act:

- (void) switchChanged:(id)sender forEvent:(UIEvent *)event {
  if(event) {
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:[[[event touchesForView:sender] anyObject] locationInView:self.tableView]];
    IFDFlightlogFormQuestions *question = [self.resultsController objectAtIndexPath:indexPath];
    NSLog(@"IFDNewFlightlogViewController_Pad:switchChanged Switch is %@ xmlAttrib %@",[sender isOn],question.XmlAttrib);
    [self.xmlResults setValue:([sender isOn])?@"true":@"false" forKey:question.XmlAttrib];
  }
}

Usage action:@selector(switchChanged: forEvent:)- Added space - no change.

Usage action:@selector(switchChanged::)- deleted forEvent- unrecognized selector.

My goal is to get indexPath in a tableView so that I can change the value in my dictionary.

My current workaround is to use only the sender information, but I need the event information:

- (void) switchChanged:(id)sender forEvent:(UIEvent *)event {
    UISwitch *switchView = (UISwitch *)sender;
    UITableViewCell *cell = (UITableViewCell *)switchView.superview;
    UITableView *tableView = (UITableView *)cell.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
    IFDFlightlogFormQuestions *question = [self.resultsController objectAtIndexPath:indexPath];
    NSLog(@"IFDNewFlightlogViewController_Pad:switchChanged Switch is %@ xmlAttrib %@",([sender isOn])?@"On":@"Off",question.XmlAttrib);
    [self.xmlResults setValue:([sender isOn])?@"true":@"false" forKey:question.XmlAttrib];        
}

Any pointers to getting event information about this?

+3
3

, no forEvent: UIControl... , .

, , .

UISwitch, NSIndexPath

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event

...

0

@selector, UISwitch.

[switchCtl addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];
+5

Another option is to set the tag property in UISwitch to match the row of your table view. If you also use multiple partitions, you can come up with some kind of solution to encode them in a single integer value. Keep in mind that with this approach, you need to update the tag every time you refresh the cell (tableView: cellForRowAtIndexPath :), because the row can change when the cells are reused.

+2
source

All Articles