Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用UIApplication.sharedApplication()发送匿名操作。Swift 3中的sendAction_Ios_Objective C_Swift_Swift2.2 - Fatal编程技术网

Ios 使用UIApplication.sharedApplication()发送匿名操作。Swift 3中的sendAction

Ios 使用UIApplication.sharedApplication()发送匿名操作。Swift 3中的sendAction,ios,objective-c,swift,swift2.2,Ios,Objective C,Swift,Swift2.2,我一直在使用sendAction方法和nil目标来将单元格(UITableViewCell/UICollectionViewCell)中的操作传达给视图控制器,而不是实现委托 自从Swift 2.2以来,选择器的语法已经更新,我得到了一些警告。新的#选择器语法坚持指定selectorname,后跟classname。如果我提到类名,那么将目标设置为nil没有任何意义 有解决办法吗 class RedeemCell: UICollectionViewCell { @IBActio

我一直在使用
sendAction
方法和
nil
目标
来将
单元格(UITableViewCell/UICollectionViewCell)
中的操作传达给
视图控制器
,而不是实现委托

自从Swift 2.2以来,选择器的语法已经更新,我得到了一些警告。新的
#选择器
语法坚持指定
selectorname
,后跟
classname
。如果我提到类名,那么将目标设置为nil没有任何意义

有解决办法吗

    class RedeemCell: UICollectionViewCell { 
    @IBAction func redeemAction(sender: AnyObject) {                 
      UIApplication.sharedApplication().sendAction("updateCartWithTotalAmountPayableWithDiscount:", to: nil, from: self, forEvent: nil)
        } 
    }

class CartVC: UIViewController {

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell: UICollectionViewCell?

            cell = collectionView.dequeueReusableCellWithReuseIdentifier("redeempoints", forIndexPath: indexPath)


        return cell!;
    }

func updateCartWithTotalAmountPayableWithDiscount(sender: AnyObject) {
        print("this will be called as the action movies through responderchain and encounters this method");
    }
}

为什么要为此使用
sendAction
,而不是使用类似
NSNotificationCenter

您可以使用
Selector()
从Objective-C样式的字符串创建选择器。为避免出现“使用#选择器”的警告,请使用参数存储选择器名称并将其传递给
selector()
,而不是直接传递字符串文本

let selectorName = "updateCartWithTotalAmountPayableWithDiscount:"
UIApplication.sharedApplication().sendAction(Selector(selectorName), to: nil, from: self, forEvent: nil)
您可以(也可能应该)通过
#选择器(CartVC.updateCartWithTotalAmountPayableWithDiscount)
。当您这样做时,您的意思是希望为
updateCart…
类定义的
updateCart…
函数使用选择器,而不是由其他类定义的
updateCart…
函数


这与将
nil
作为目标传递的含义不同-您在
#selector
表达式中命名的类根据定义它的类来澄清您要发送的消息是什么。目标是关于您向谁发送消息。当您为目标选择nil时,您的意思是消息应该转到可以处理它的第一个对象,它可以是
CartVC
的多个实例之一,或者是接受相同消息的另一个类的实例

这样做有点晚了,但是现在您应该定义一个对objective-c运行时可见的协议并使用该协议。我想UITableViewDelegate现在看起来更漂亮了,不是吗?:)

你能分享你的代码来解释你到目前为止是如何使用它的吗?@rptwsthi用代码更新了这个问题是的,我刚刚检查了它,不幸的是我没有解决这个问题的方法。可能有人来救我。祝你好运。如果sendAction无法达到要求,我可以使用delegate或NSNotificationCenter。我的疑问是,有了新的语法,我们将永远无法像我上面所说的那样使用sendAction。考虑到它不能像你上面使用sendAction那样工作,也许现在是时候重新考虑你过去的决定,并使用正确的方法来处理它了?NSNotificationCenter是用于在层次结构下广播事件的。sendAction(和委派)用于向层次结构上发送事件。或者您可以在字符串周围加一个括号:
Selector((“updateCartWithTotalAmountPayableWithDiscount:”)