在這個論壇中使用了精彩的帖子,我在tableView中創建了一個作爲accessoryView的開關。當觸摸開關時,我的動作(switchChanged)被調用。只有發件人有一個有效的值,事件是0x0。addTarget:action:forControlEvents - TableView中的UISwitch - 發件人ok,事件總是0x0
添加目標到SWITCHVIEW:
[switchView addTarget:self action:@selector(switchChanged:forEvent:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];
行動:
- (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])[email protected]"true":@"false" forKey:question.XmlAttrib];
}
}
使用action:@selector(switchChanged: forEvent:)
- 增值空間 - 沒有變化。
使用action:@selector(switchChanged::)
- 刪除forEvent
- 無法識別的選擇器。
我的目標是獲得indexPath到tableView,以便我可以更改我的字典中的值。
我目前的解決辦法是使用僅僅是發件人信息,但我想事件信息:
- (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])[email protected]"On":@"Off",question.XmlAttrib);
[self.xmlResults setValue:([sender isOn])[email protected]"true":@"false" forKey:question.XmlAttrib];
}
在得到這個事件信息任何指針?
好的,明白了。謝謝你,亞當。我將繼續使用該解決方法。我喜歡UISwitch的子類化想法,它現在在我的待辦事項列表中。 – Kent 2011-05-03 00:00:36