ios - 根据按钮选择状态将无法识别的选择器发送到实例

标签 ios button swift selector

我在 UIControllerView 中设置了一个按钮,用于使用 Parse.com 后端关注特定用户。

我想要这种行为:

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

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

我的问题是:

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

如果按钮未被选中(因为用户不是 friend )=>

 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 逻辑,只是为了测试当我按下按钮时对选定状态的更改是否正常工作但出现相同的错误。

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

最佳答案

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

Swift 是一种动态语言,因此可以在运行时创建方法。因此,您需要将它期望的方法名称(“选择器”)传递给它。

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

检查你在哪里分配按钮监听器,它看起来像这样:

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

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

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

另请注意: 如果您使用的是界面生成器,请检查重命名时是否遗留了遗留代码。如果连接了一个 IBAction,然后从代码中删除了它,在 Interface Builder 中打开对象,单击最右边的(箭头)选项卡,然后删除内部 Action 的修饰。

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

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

enter image description here

关于ios - 根据按钮选择状态将无法识别的选择器发送到实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26491656/

相关文章:

ios - Facebook 用户信息以及联系电话

需要指导的 Android(学生 cw)

swift - 以编程方式在indexpath.item处呈现一个UIViewController(嵌入在UITabBarController中)

ios - 什么是FNFPlayer?它从后台线程访问UIApplication applicationState

iphone - iOS 基本 FTP 设置;读写流

.net - 如何在 WPF 中更改按钮的制表位虚线边框颜色?

html - 为提交、取消、删除、操作等做 html 按钮的正确方法是什么?

ios - swift 位置 : Fire every X Meters a Notification

ios - 如何在 swift playground 中制作自定义 tableview 单元格?

ios - 如何让数组中的每个字符串成为其自己的 UITableViewCell,每个都在其自己的部分中?