Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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 如何将多个参数传递给Swift#selector?_Ios_Swift - Fatal编程技术网

Ios 如何将多个参数传递给Swift#selector?

Ios 如何将多个参数传递给Swift#selector?,ios,swift,Ios,Swift,我想将第二个参数传递给来自另一个函数的选择器函数: func bindToKeyboardNew(constraint: NSLayoutConstraint) { // <- this parameter NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:constraint:)), name: UIResponder.keyboardWillShowNotif

我想将第二个参数传递给来自另一个函数的选择器函数:

func bindToKeyboardNew(constraint: NSLayoutConstraint) { // <- this parameter 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:constraint:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}

@objc func keyboardWillShow(_ notification: NSNotification, constraint: NSLayoutConstraint) // <- To This selector {

}

func bindToKeyboardNew(constraint:NSLayoutConstraint){/传递数据的更简单方法是创建自定义类。
示例我需要通过
UITapGestureRecognizer
传递数据

首先,创建一个自定义
UITapGestureRecognitor
,并定义一个作为数据的实例

class CustomTapGesture: UITapGestureRecognizer {
    var data: YourData
}
选择器功能

@objc func tap(_ sender: UITapGestureRecognizer) {
    if let sender = sender as? CustomTapGesture {
       yourData = sender.data
       // do something with your data
    }
}

正如@rmaddy在评论中提到的,这不能按照您要求的方式来做

在这部分

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:constraint:)), name: UIResponder.keyboardWillShowNotification, object: nil)
您无法控制选择器的确切发送方式(以及参数)

UIKit在其私有实现中内部执行类似的操作(让我们暂时忽略一下,它的实现不是真正快速的,这在这里并不重要):

这意味着选择器已经被发送,并且无法向可选用户信息中添加额外内容(如果
.post(…)
确实发生在代码中,您可以这样做,但这里的情况并非如此)

您需要一个替代方法来访问键盘选择器显示/隐藏处理程序中当前的
NSLayoutConstraint
对象。可能它应该是
ViewController
中的一个属性,可能是
AppDelegate
的某个状态,也可能是完全不同的某个状态,不知道代码的剩余部分

编辑:根据您添加的评论,我假设您有如下内容:

class ViewController: UIViewController {
    @IBOutlet var constraint: NSLayoutConstraint?
}
如果是这样,您只需访问VC中选择器处理程序中的约束:

class ViewController: UIViewController {
    @IBOutlet var constraint: NSLayoutConstraint?
    @objc func keyboardWillShow(_ notification: NSNotification) {
       //do something with the constraint 
       print(constraint)
    }
}

还有一个专用的
UIKeyboardWillChangeFrameNotification
也许它可以为您提供开箱即用的内容。请看答案,您不能。您需要另一种方法来访问
keyboardWillShow
函数中的约束。解决方法是什么?我正在尝试开发一个扩展,将视图绑定到k跳板很容易;(您可以在通知中绑定userInfo dict中的视图。)selector@abuul Hassan它就像普通的绑定一样,它不能正常工作,它应该正常工作,问题是用有效的方法发送一个对象,我认为除了userInfo dict之外,没有其他有效的方法来传递任何对象。我理解,但我使用NotificationCenter的情况是什么NSNotification和NSNotification它们是两种不同的东西Su可以创建一个自定义类,
NotificationCenter
NSNotification
的实例是您的数据。
class custom{var instance1:NotificationCenter?var instance2:NSNotification?}
它在我的ViewcontrollerI中。我知道。但这不是我想要的。我想要一个专门的类,这样我就不必把那大块代码放在里面。但我已经找到了解决方案(看看我的另一个问题)。无论如何,谢谢。
class ViewController: UIViewController {
    @IBOutlet var constraint: NSLayoutConstraint?
}
class ViewController: UIViewController {
    @IBOutlet var constraint: NSLayoutConstraint?
    @objc func keyboardWillShow(_ notification: NSNotification) {
       //do something with the constraint 
       print(constraint)
    }
}