将变量从Gesture Recognizer函数传递给IBAction

时间:2016-10-04 07:08:54

标签: swift uicollectionview uigesturerecognizer return-type ibaction

我有一个简单的应用程序,其中包含CollectionView和其中的项目。 长按cell弹出式UIView会显示TextField,并可选择将其保存在与array对应的cell中。

以下是代码(在buttons方法中已正确添加gesturesviewDidLoad()):

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

  • 我尝试将Button的Selector功能代码放在Long Press Gesture Function中,以便它的返回值很容易获得。但是,我无法调用Selector函数,因为它在另一个函数中。

  • 此外,我尝试返回Long Press Gesture Function的值并在“保存”按钮的IBAction中使用它。但是,为此,我需要再次调用handleLongPress,然后将longPressedPoint检测为内部“保存”按钮。因此,indexPathOfLongPressedCellnil,应用程序崩溃。

有人可以帮助我......

2 个答案:

答案 0 :(得分:2)

假设您想要获取所选单元格的行并将其分配给全局变量Date,则不需要让const d1 = new Date('1970-01-18T00:00:00+00:00'); // Numeric value as the number of milliseconds since UNIX epoch. const time = d1.getTime(); db.executeAsync(insertQuery, [ id, d1 ], { prepare: true } ); // ... db.executeAsync(selectQuery, [ id ], { prepare: true }) .then((result) => { console.log('Are equal?', time, result.first()['date_created'].getTime()) }) 返回Int。

注意:这是一个Swift 3代码(概念相同):

rowOfLongPressedItem

此外,您无需让handleLongPress获取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是可选的,您应该确保它仍然是零(您可以使用Early Exit方法):

rowOfLongPressedItem

答案 1 :(得分:0)

两个函数都可以使用

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
}