我有一个简单的应用程序,其中包含 CollectionView
和项目。
长按 cell
会出现一个弹出式 UIView
,其中包含一个 TextField
和一个将其保存在相应的 array
中的选项到单元格
。
这是代码(按钮
和手势
已正确添加到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
}
}
尝试通过几种方式解决问题:
在 View Controller 中定义一个变量,希望该函数返回值并将其保存在全局变量中。 但是,后来从Apple那里发现 “函数的访问级别不能高于其参数类型和返回类型,因为该函数可以在其构成类型对周围代码不可用的情况下使用。” https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html
我尝试将按钮的选择器功能代码放在长按手势功能中,以便轻松获得它的返回值。但是,我无法调用 Selector 函数,因为它在另一个函数中。
此外,我尝试返回长按手势功能的值并在保存按钮的 IBAction 中使用它。但是,为此我需要再次调用
handleLongPress
然后longPressedPoint
被检测为在 Save 按钮内。因此,indexPathOfLongPressedCell
为nil
,应用程序崩溃。
谁能帮帮我...
最佳答案
假设您想要获取所选单元格的行并将其分配给全局变量 rowOfLongPressedItem
,您不需要让 handleLongPress
返回一个 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
}
关于swift - 将变量从 Gesture Recognizer 函数传递到 IBAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39846201/