[英]Swift Variable used within its own initial value while using #selector
我正在嘗試從選擇器(操作)調用我的方法,但在其自己的初始值中使用了錯誤變量下面是我的代碼片段
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(sendRequest(tapGestureRecognizer, userID)))
以下是我正在調用的方法
@objc func sendRequest(tapGestureRecognizer: UITapGestureRecognizer, identifer: String) {
print("hello world")
}
我的方法接受 2 個參數。 我不知道怎么稱呼它。 我當前調用 sendRequestMethod 的方式是拋出錯誤。 請幫我解決它。
當這些功能在 UIView 中時,就像 GestureRecognizers 通常那樣,那么您可以進行最熱門的測試並找到 UIView 在您的觸摸下。
考慮到這不是捕捉觸摸視圖的理想方式。 由於每個 UIView 還為您提供了一個.tag ,您可以設置一個編號並使用它來假設 UIView 適合哪些人。 查看示例
class checkCheckView : UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(sendRequest(recognizer:identifer:)) )
@objc func sendRequest(recognizer:UITapGestureRecognizer, identifer:String) {
print("hello world")
// your problem will be, how is identifier passed in here?
}
let PanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panning(gesture:)))
@objc func panning(gesture:UIPanGestureRecognizer) {
//print("pan-translation %@", gesture.translation(in: self).debugDescription)
let location = gesture.location(ofTouch: 0, in: self)
//print("pan-location %@",location.debugDescription);
let hitview = self.hitTest(location, with: nil)
if hitview != nil {
let UserIDTag = hitview?.tag ?? 0
print("userId by UIView tag",UserIDTag)
}
}
}
提示:更容易的是繼承 UIView 並准備一個屬性來保存分配完成時給定的 UserID。 addtarget: action:#selector()
到他們每個人。 調用操作/選擇器方法並傳入發送者,因為您知道發送者的類型 (YourUserUIView),所以您可以找到用戶 ID。
使用 GestureRecognizer,您傾向於編寫識別代碼,如果您的 UI 越復雜,識別代碼就會變得越復雜,而不僅僅是將對象傳遞給告訴您正在查找的內容的操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.