将变量从 Gesture Recognizer 函数传递到 IBAction
Passing a variable from Gesture Recognizer function to an IBAction
我有一个简单的应用程序,里面有 CollectionView
和项目。
长按 cell
会出现一个 UIView
弹出窗口,其中包含 TextField
和一个将其保存在与 cell
.[=25= 相对应的 array
中的选项]
代码如下(buttons
和 gestures
已在 viewDidLoad()
方法中正确添加):
class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var longPressedPoint: CGPoint?
public var rowOfLongPressedItem: Int? = nil
func handleLongPress(longPressRecognizer: UILongPressGestureRecognizer) -> Int {
print("LONG PRESS Gesture Recognized")
notePopup.hidden = false
longPressedPoint = longPressRecognizer.locationInView(longPressRecognizer.view)
var indexPathOfLongPressedCell = self.itemCollectionView.indexPathForItemAtPoint(longPressedPoint!)
rowOfLongPressedItem = (indexPathOfLongPressedCell?.row)
print("rowOfLongPressedItem -> .\(rowOfLongPressedItem)")
return rowOfLongPressedItem!
}
func saveNoteButtonTapped(rowOfLongPressedItem: Int) {
print("rowOfLongPressedItem when Save button is tapped -> .\(rowOfLongPressedItem)")
//Can’t go further down as rowOfLongPressedItem is NOT available from “handleLongPress” function…
var selectedItem = ItemsList[rowOfLongPressedItem]
selectedItem.counts += 1
var latest = selectedItem.counts - 1
selectedItem.timestamp.append(NSDate())
selectedItem.note.append(noteTextField.text)
ItemsList[rowOfLongPressedItem] = selectedItem
print(".\(selectedItem.title) has been tapped .\(selectedItem.counts)")
print("The latest tap on .\(selectedItem.title) is at .\(selectedItem.timestamp[latest])")
print("The note .\(noteTextField.text) has been added")
notePopup.hidden = true
}
}
尝试通过几种方式解决问题:
在视图控制器中定义一个变量,希望该函数将 return 值并将其保存在全局变量中。
但是,后来从Apple那里发现
"A function cannot have a higher access level than its parameter types and return type, because the function could be used in situations where its constituent types are not available to the surrounding code."
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html
我尝试将按钮的选择器功能代码放入长按手势功能中,以便轻松获得它的 return 值。但是,我无法调用 Selector 函数,因为它在另一个函数中。
此外,我尝试了 returning 长按手势功能的值并在保存按钮的 IBAction 中使用它。但是,为此我需要再次调用 handleLongPress
然后 longPressedPoint
被检测为在保存按钮内。因此,indexPathOfLongPressedCell
是 nil
并且应用程序崩溃。
有人可以帮帮我吗...
rowOfLongPressedItem
可用于两个函数。您不需要将其作为 saveNoteButtonTapped
.
的参数
var rowOfLongPressedItem: Int? = nil
func handleLongPress(longPressRecognizer: UILongPressGestureRecognizer) -> Int {
print("LONG PRESS Gesture Recognized")
notePopup.hidden = false
longPressedPoint = longPressRecognizer.locationInView(longPressRecognizer.view)
var indexPathOfLongPressedCell = self.itemCollectionView.indexPathForItemAtPoint(longPressedPoint!)
rowOfLongPressedItem = (indexPathOfLongPressedCell?.row)
print("rowOfLongPressedItem -> .\(rowOfLongPressedItem)")
return rowOfLongPressedItem!
}
func saveNoteButtonTapped() {
guard let row = rowOfLongPressedItem else {
return // rowOfLongPressedItem was nil
}
print("rowOfLongPressedItem when Save button is tapped -> .\(row)")
var selectedItem = ItemsList[row]
selectedItem.counts += 1
var latest = selectedItem.counts - 1
selectedItem.timestamp.append(NSDate())
selectedItem.note.append(noteTextField.text)
ItemsList[row] = selectedItem
print(".\(selectedItem.title) has been tapped .\(selectedItem.counts)")
print("The latest tap on .\(selectedItem.title) is at .\(selectedItem.timestamp[latest])")
print("The note .\(noteTextField.text) has been added")
notePopup.hidden = true
}
假设你想获取所选单元格的行并将其分配给一个全局变量rowOfLongPressedItem
,你不需要让handleLongPress
returns一个Int.
注意:这是一个Swift 3代码(概念相同):
public var rowOfLongPressedItem: Int? = nil
override func viewDidLoad() {
//...
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.assignRowOfLongPressedItem))
itemCollectionView.addGestureRecognizer(longPressRecognizer)
//...
}
func assignRowOfLongPressedItem(longPressRecognizer: UILongPressGestureRecognizer) {
let longPressedPoint = longPressRecognizer.location(in: longPressRecognizer.view)
var indexPathOfLongPressedCell = self.itemCollectionView.indexPathForItem(at: longPressedPoint)
rowOfLongPressedItem = (indexPathOfLongPressedCell?.row)
// if you long press the first row -for example-, the output should be: "rowOfLongPressedItem -> .Optional(0)"
print("rowOfLongPressedItem -> .\(rowOfLongPressedItem)")
}
此外,您不需要让 saveNoteButtonTapped
获取 rowOfLongPressedItem
参数。请注意,rowOfLongPressedItem
是可选的,您应该确保它仍然不是 nil(您可以使用 Early Exit 方法):
func saveNoteButtonTapped(sender: UIButton) {
guard let selectedCellRow = rowOfLongPressedItem else {
print("rowOfLongPressedItem is nil!!")
return
}
print("rowOfLongPressedItem when Save button is tapped -> .\(selectedCellRow)")
var selectedItem = ItemsList[selectedCellRow]
selectedItem.counts += 1
var latest = selectedItem.counts - 1
selectedItem.timestamp.append(NSDate())
selectedItem.note.append(noteTextField.text)
ItemsList[row] = selectedItem
print(".\(selectedItem.title) has been tapped .\(selectedItem.counts)")
print("The latest tap on .\(selectedItem.title) is at .\(selectedItem.timestamp[latest])")
print("The note .\(noteTextField.text) has been added")
notePopup.hidden = true
}
我有一个简单的应用程序,里面有 CollectionView
和项目。
长按 cell
会出现一个 UIView
弹出窗口,其中包含 TextField
和一个将其保存在与 cell
.[=25= 相对应的 array
中的选项]
代码如下(buttons
和 gestures
已在 viewDidLoad()
方法中正确添加):
class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var longPressedPoint: CGPoint?
public var rowOfLongPressedItem: Int? = nil
func handleLongPress(longPressRecognizer: UILongPressGestureRecognizer) -> Int {
print("LONG PRESS Gesture Recognized")
notePopup.hidden = false
longPressedPoint = longPressRecognizer.locationInView(longPressRecognizer.view)
var indexPathOfLongPressedCell = self.itemCollectionView.indexPathForItemAtPoint(longPressedPoint!)
rowOfLongPressedItem = (indexPathOfLongPressedCell?.row)
print("rowOfLongPressedItem -> .\(rowOfLongPressedItem)")
return rowOfLongPressedItem!
}
func saveNoteButtonTapped(rowOfLongPressedItem: Int) {
print("rowOfLongPressedItem when Save button is tapped -> .\(rowOfLongPressedItem)")
//Can’t go further down as rowOfLongPressedItem is NOT available from “handleLongPress” function…
var selectedItem = ItemsList[rowOfLongPressedItem]
selectedItem.counts += 1
var latest = selectedItem.counts - 1
selectedItem.timestamp.append(NSDate())
selectedItem.note.append(noteTextField.text)
ItemsList[rowOfLongPressedItem] = selectedItem
print(".\(selectedItem.title) has been tapped .\(selectedItem.counts)")
print("The latest tap on .\(selectedItem.title) is at .\(selectedItem.timestamp[latest])")
print("The note .\(noteTextField.text) has been added")
notePopup.hidden = true
}
}
尝试通过几种方式解决问题:
在视图控制器中定义一个变量,希望该函数将 return 值并将其保存在全局变量中。 但是,后来从Apple那里发现 "A function cannot have a higher access level than its parameter types and return type, because the function could be used in situations where its constituent types are not available to the surrounding code." https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html
我尝试将按钮的选择器功能代码放入长按手势功能中,以便轻松获得它的 return 值。但是,我无法调用 Selector 函数,因为它在另一个函数中。
此外,我尝试了 returning 长按手势功能的值并在保存按钮的 IBAction 中使用它。但是,为此我需要再次调用
handleLongPress
然后longPressedPoint
被检测为在保存按钮内。因此,indexPathOfLongPressedCell
是nil
并且应用程序崩溃。
有人可以帮帮我吗...
rowOfLongPressedItem
可用于两个函数。您不需要将其作为 saveNoteButtonTapped
.
var rowOfLongPressedItem: Int? = nil
func handleLongPress(longPressRecognizer: UILongPressGestureRecognizer) -> Int {
print("LONG PRESS Gesture Recognized")
notePopup.hidden = false
longPressedPoint = longPressRecognizer.locationInView(longPressRecognizer.view)
var indexPathOfLongPressedCell = self.itemCollectionView.indexPathForItemAtPoint(longPressedPoint!)
rowOfLongPressedItem = (indexPathOfLongPressedCell?.row)
print("rowOfLongPressedItem -> .\(rowOfLongPressedItem)")
return rowOfLongPressedItem!
}
func saveNoteButtonTapped() {
guard let row = rowOfLongPressedItem else {
return // rowOfLongPressedItem was nil
}
print("rowOfLongPressedItem when Save button is tapped -> .\(row)")
var selectedItem = ItemsList[row]
selectedItem.counts += 1
var latest = selectedItem.counts - 1
selectedItem.timestamp.append(NSDate())
selectedItem.note.append(noteTextField.text)
ItemsList[row] = selectedItem
print(".\(selectedItem.title) has been tapped .\(selectedItem.counts)")
print("The latest tap on .\(selectedItem.title) is at .\(selectedItem.timestamp[latest])")
print("The note .\(noteTextField.text) has been added")
notePopup.hidden = true
}
假设你想获取所选单元格的行并将其分配给一个全局变量rowOfLongPressedItem
,你不需要让handleLongPress
returns一个Int.
注意:这是一个Swift 3代码(概念相同):
public var rowOfLongPressedItem: Int? = nil
override func viewDidLoad() {
//...
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.assignRowOfLongPressedItem))
itemCollectionView.addGestureRecognizer(longPressRecognizer)
//...
}
func assignRowOfLongPressedItem(longPressRecognizer: UILongPressGestureRecognizer) {
let longPressedPoint = longPressRecognizer.location(in: longPressRecognizer.view)
var indexPathOfLongPressedCell = self.itemCollectionView.indexPathForItem(at: longPressedPoint)
rowOfLongPressedItem = (indexPathOfLongPressedCell?.row)
// if you long press the first row -for example-, the output should be: "rowOfLongPressedItem -> .Optional(0)"
print("rowOfLongPressedItem -> .\(rowOfLongPressedItem)")
}
此外,您不需要让 saveNoteButtonTapped
获取 rowOfLongPressedItem
参数。请注意,rowOfLongPressedItem
是可选的,您应该确保它仍然不是 nil(您可以使用 Early Exit 方法):
func saveNoteButtonTapped(sender: UIButton) {
guard let selectedCellRow = rowOfLongPressedItem else {
print("rowOfLongPressedItem is nil!!")
return
}
print("rowOfLongPressedItem when Save button is tapped -> .\(selectedCellRow)")
var selectedItem = ItemsList[selectedCellRow]
selectedItem.counts += 1
var latest = selectedItem.counts - 1
selectedItem.timestamp.append(NSDate())
selectedItem.note.append(noteTextField.text)
ItemsList[row] = selectedItem
print(".\(selectedItem.title) has been tapped .\(selectedItem.counts)")
print("The latest tap on .\(selectedItem.title) is at .\(selectedItem.timestamp[latest])")
print("The note .\(noteTextField.text) has been added")
notePopup.hidden = true
}