Xcode Storyboard - UITableViewCell UISwitch值已更改

时间:2014-07-10 22:49:50

标签: xcode uitableview storyboard uiswitch

我使用的是Xcode故事板,并且有一个带有UITableView的View Controller,而UITableView又有一个自定义的UITableViewCell。

UITableViewCell中有一个UISwitch,我想知道值何时发生了变化。我有一个IBAction,当值改变时成功触发

- (IBAction)updateSwitchAtIndexPath:(NSIndexPath *)indexPath
{
     NSLog(@"In SettingsViewcontroller updateSwitchAtIndexPath row=|%d \n", indexPath.row);
} 

然而,当它触发应用终止时

2014-07-10 23:38:10.037 reactor[2572:60b] -[UISwitch row]: unrecognized selector sent to instance 0x944b7e0
2014-07-10 23:38:10.072 reactor[2572:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UISwitch row]: unrecognized selector sent to instance 0x944b7e0'
*** First throw call stack:
(
    0   CoreFoundation                      0x01a281e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x015758e5 objc_exception_throw + 44
    2   CoreFoundation                      0x01ac5243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275   
    3   CoreFoundation                      0x01a1850b ___forwarding___ + 1019
    4   CoreFoundation                      0x01a180ee _CF_forwarding_prep_0 + 14
    5   positivity                          0x00003807 -[SettingsViewController updateSwitchAtIndexPath:] + 87
    6   libobjc.A.dylib                     0x0158782b -[NSObject performSelector:withObject:] + 70
    7   UIKit                               0x002373b9 -[UIApplication sendAction:to:from:forEvent:] + 108
    8   UIKit                               0x00237345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    9   UIKit                               0x00338bd1 -[UIControl sendAction:to:forEvent:] + 66
    10  UIKit                               0x00338fc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
    11  UIKit                               0x004c97a5 __31-[UISwitch _handleLongPressNL:]_block_invoke + 85
    12  UIKit                               0x004c8666 -[_UISwitchInternalViewNeueStyle1 _setPressed:on:animated:shouldAnimateLabels:completion:] + 243
    13  UIKit                               0x004c9681 -[UISwitch _handleLongPressNL:] + 286
    14  UIKit                               0x005d14f4 _UIGestureRecognizerSendActions + 230
    15  UIKit                               0x005d0168 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 383
    16  UIKit                               0x005d1bdd -[UIGestureRecognizer _delayedUpdateGesture] + 60
    17  UIKit                               0x005d513d ___UIGestureRecognizerUpdate_block_invoke + 57
    18  UIKit                               0x005d50be _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 317
    19  UIKit                               0x005cb7ac _UIGestureRecognizerUpdate + 199
    20  UIKit                               0x00276a5a -[UIWindow _sendGesturesForEvent:] + 1291
    21  UIKit                               0x00277971 -[UIWindow sendEvent:] + 1021
    22  UIKit                               0x002495f2 -[UIApplication sendEvent:] + 242
    23  UIKit                               0x00233353 _UIApplicationHandleEventQueue + 11455
    24  CoreFoundation                      0x019b177f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    25  CoreFoundation                      0x019b110b __CFRunLoopDoSources0 + 235
    26  CoreFoundation                      0x019ce1ae __CFRunLoopRun + 910
    27  CoreFoundation                      0x019cd9d3 CFRunLoopRunSpecific + 467
    28  CoreFoundation                      0x019cd7eb CFRunLoopRunInMode + 123
    29  GraphicsServices                    0x03a1c5ee GSEventRunModal + 192
    30  GraphicsServices                    0x03a1c42b GSEventRun + 104
    31  UIKit                               0x00235f9b UIApplicationMain + 1225
    32  positivity                          0x00005dbd main + 141
    33  libdyld.dylib                       0x0206f701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

UISwitch没有名为" row的属性。"设置IBOutlet的方式实际上是将UISwitch对象作为参数而不是NSIndexPath。

这里更好的做法是使用委托模式。这样,自定义单元格可以通知UIViewController(包含UITableView)已按下开关。

所以它更像是:

- (IBAction)updateSwitchAtIndexPath:(UISwitch *)mySwitch
{
    // notify the delegate about the switch update
    if ([self.delegate respondsToSelector:@selector(switchWasPressed:)]) {
        [self.delegate switchWasPressed:mySwitch];
    }
}