【问题标题】:Having a unrecognized selector sent to instance based on a button selected state根据按钮选择状态将无法识别的选择器发送到实例
【发布时间】:2014-10-21 16:28:40
【问题描述】:

我在 UIControllerView 中有一个按钮,用于通过 Parse.com 后端跟踪特定用户。

我想要这种行为:

-> 用户不是朋友,按钮状态 = 未选中,在 Parse.com 中执行以下操作 => 将按钮状态更改为选中

-> 用户是朋友,按钮状态 = 选中,在 parse.com 中执行取消关注操作 => 将按钮状态更改为未选中

我的问题是:

如果按钮被选中(因为用户已经是朋友)=> 一切正常

如果按钮没有被选中(因为用户不是朋友)=>

 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Sliked.FriendsProfileViewControler followButton:]: unrecognized selector sent to instance    0x7f7f7ad63370'
  *** First throw call stack:
 (
   0   CoreFoundation                      0x000000010aabe3f5 __exceptionPreprocess + 165
   1   libobjc.A.dylib                     0x000000010c5ecbb7 objc_exception_throw + 45
   2   CoreFoundation                      0x000000010aac550d -[NSObject(NSObject)    doesNotRecognizeSelector:] + 205
   3   CoreFoundation                      0x000000010aa1d7fc ___forwarding___ + 988
   4   CoreFoundation                      0x000000010aa1d398 _CF_forwarding_prep_0 + 120
   5   UIKit                               0x000000010b3499ee -[UIApplication sendAction:to:from:forEvent:] + 75
   6   UIKit                               0x000000010b44fbd0 -[UIControl _sendActionsForEvents:withEvent:] + 467
   7   UIKit                               0x000000010b44ef9f -[UIControl touchesEnded:withEvent:] + 522
   8   UIKit                               0x000000010b38f3b8 -[UIWindow _sendTouchesForEvent:] + 735
   9   UIKit                               0x000000010b38fce3 -[UIWindow sendEvent:] + 683
  10  UIKit                               0x000000010b35cae1 -[UIApplication sendEvent:] + 246
  11  UIKit                               0x000000010b369bad _UIApplicationHandleEventFromQueueEvent + 17370
  12  UIKit                               0x000000010b345233 _UIApplicationHandleEventQueue + 1961
  13  CoreFoundation                      0x000000010a9f3ad1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
  14  CoreFoundation                      0x000000010a9e999d __CFRunLoopDoSources0 + 269
  15  CoreFoundation                      0x000000010a9e8fd4 __CFRunLoopRun + 868
  16  CoreFoundation                      0x000000010a9e8a06 CFRunLoopRunSpecific + 470
  17  GraphicsServices                    0x000000010db299f0 GSEventRunModal + 161
  18  UIKit                               0x000000010b348550 UIApplicationMain + 1282
  19  Sliked                              0x0000000109273a4e top_level_code + 78
  20  Sliked                              0x0000000109273a8a main + 42
  21  libdyld.dylib                       0x000000010cdc6145 start + 1
   )
   libc++abi.dylib: terminating with uncaught exception of type NSException

我的代码如下所示:

    @IBOutlet var followUnfollowButton: UIButton!

    @IBAction func followUnfollow (sender: AnyObject) {



    let user:PFObject = userPassed
    let currentUser = PFUser.currentUser()
    let relation : PFRelation = currentUser.relationForKey("KfriendsRelation")

    if  !followUnfollowButton.selected {

        followUnfollowButton.selected = true

        followUnfollowButton.setTitle("Unfollow", forState: UIControlState.Selected)
        followUnfollowButton.backgroundColor = UIColor(red: 117/255, green: 201/255, blue: 223/255, alpha: 1.0)

        relation.addObject(user)

        PFUser.currentUser().saveInBackgroundWithBlock { (succeed:Bool, error: NSError!) -> Void in
            if error != nil {
                println("not working")
            }



            println("following user succes")

        }

    }

    else  {


        followUnfollowButton.selected = false
        followUnfollowButton.backgroundColor = UIColor(red: 248/255, green: 255/255, blue: 9/255, alpha: 1.0)


        relation.removeObject(user)

        PFUser.currentUser().saveInBackgroundWithBlock{   (succeed:Bool, error: NSError!) -> Void in

            if error != nil {
                println(error)
                println("can't unfollow user")
            }
        }
    }


}

我试图删除 Parse 逻辑,只是为了测试当我按下按钮时对所选状态的更改是否正常工作但有相同的错误。

这很奇怪,我错过了什么?

【问题讨论】:

  • "sent to instance..." 接下来是什么意思。这几乎是您切断的错误消息中最重要的部分。
  • 检查我编辑的帖子
  • 您在某处调用函数followButton:,但您的代码中似乎没有该函数。我建议对“followButton”进行项目搜索
  • 您正在启用某种事件处理程序并指定followButton: 作为处理事件的方法。但它被“发送”到视图控制器,而不是预期的对象。

标签: ios button swift selector


【解决方案1】:

此错误意味着找不到方法名称(称为“选择器”)。从字面上看,正在侦听点击事件的对象没有它应该具有的适当命名的方法(“followButton:”)。

Swift 是一种动态语言,因此可以在运行时创建方法。所以你需要将它期望的方法的名称('Selector')传递给它。

在这种情况下,您的点击事件侦听器需要一个名为“followButton:”的方法

检查您分配按钮侦听器的位置,它看起来像这样:

_tapRecoginizer = UITapGestureRecognizer(target: self, action:"tap:")

对于您的情况,您正在寻找动作显示“followButton:”的位置。确保目标具有该方法和适当的侦听器(在您的情况下可能是 UITapGesture)

请记住,“followButton:”与“followButton”不同。 “:”表示该方法期望将对象作为参数传递,因此请确保不要忘记“:”

还要记住: 如果您使用的是界面生成器,请检查您在重命名时是否保留了遗留代码。如果连接了 IBAction,然后从代码中将其删除,在 Interface Builder 中打开对象,单击最右侧(箭头)选项卡,然后删除 touch up inside action。

从代码中删除不会更新界面构建器中的对象,因此请确保您也删除了它在事件侦听器中的修饰!

听起来您连接了一个 IBAction 并对其进行了重命名,现在它正在寻找一个不在脚本中的方法。因此,请按照以下步骤进行修复。删除界面生成器中的引用,重新连接(控制拖动),然后重新命名。

【讨论】:

  • 我没有 UITapGestureRecognizer,我只是用我的故事板使用控制拖动方法设置了一个 IBAction。
  • 我可以看看您的名为“followButton:”的 IBAction。没看到
  • 这是我删除的东西,我在连接检查器中找不到它。我该怎么办?
  • 看起来就像我之前说的,监听followButton的脚本:没有那个方法可以实现。
  • 我更新了我的答案,你证实了我的怀疑。如果我的答案不清楚,请告诉我,否则请按照这些步骤操作,您可以轻松解决。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-04
  • 2015-06-16
  • 2019-08-28
  • 2012-07-24
相关资源
最近更新 更多