【问题标题】:Xcode Storyboard - UITableViewCell UISwitch Value ChangedXcode Storyboard - UITableViewCell UISwitch 值已更改
【发布时间】:2014-07-10 22:49:50
【问题描述】:

我正在使用 Xcode 故事板,并且有一个视图控制器,该控制器具有一个 UITableView,而 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

任何帮助将不胜感激

【问题讨论】:

    标签: xcode uitableview storyboard uiswitch


    【解决方案1】:

    UISwitch 没有名为“行”的属性。设置 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];
        }
    }
    

    【讨论】:

    • 感谢您的快速回复,但我如何判断选择了哪一行?
    • 从技术上讲,您没有选择行。为什么要知道选择了哪一行?您已经知道哪个单元格包含 UISwitch。
    • 我怎么知道的?每行都有一个uiswitch。
    • 好的,这很有帮助。我看不到您在原始帖子中明确指出的地方。你肯定有几件事可以做。一种方法是继承 UISwitch 并让它保留对其所属单元格的引用(作为属性)。然后,您将能够通过上述委托方法访问单元格。
    猜你喜欢
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    • 2019-08-26
    • 1970-01-01
    相关资源
    最近更新 更多