调用 UIPasteBoard 的问题

Issues with call of UIPasteBoard

我的键盘有问题。

我创建了一个视图作为我项目的输入源。

为了做到这一点,我使用 UIPasteBoard class 在 textView 上书写。

这个 inputView 有一个 collectionView 对象,当我按下一个单元格时没有任何反应。

但是,如果我复制一些东西,这很好用并且可以写。我该如何解决?

这是我的代码:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    //Load calculated chord
    let step1 = defaults.string(forKey: "step1") ?? ""
    let step2 = defaults.string(forKey: "step2") ?? ""
    let step3 = defaults.string(forKey: "step3") ?? ""


    switch indexPath.row {
    case indexPath.row:
                cellPressedSound()
                //add selected chord
                // Get a reference to the system pasteboard
                let lPasteBoard = UIPasteboard.general

                // Save the current pasteboard contents so we can restore them later
                let lPasteBoardItems = lPasteBoard.items

                // Update the system pasteboard with my string
                lPasteBoard.string = chromaticScale[indexPath.row] + step1 + step2 + step3

                // Paste the pasteboard contents at current cursor location
                self.chords.paste(self)

                // Restore original pasteboard contents
                lPasteBoard.items = lPasteBoardItems


    default:
        break
    }
}

正如我之前指定的,当我尝试写一些东西时,除非我不复制随机的东西,否则什么也不会发生。

我自己找到了解决方案。问题是我没有创建剪贴板。因此,要写入选定的单元格,我必须将其按下两次。所以我首先清理 iPhone 剪贴板以保存之前复制的内容以防万一。

// Save the current pasteboard contents so we can restore them later
//let lPasteBoardItems = lPasteBoard.items

//clean clipboard and create path to paste data
//UIPasteboard.general.string?.removeAll()
UIPasteboard.general.string = "r"
                                        
// Get a reference to the system pasteboard
let lPasteBoard = UIPasteboard.general
                                        
// Update the system pasteboard adding selected chord
lPasteBoard.string = chromaticScale[indexPath.row] + step1 + step2 + step3
                                        
// Paste the pasteboard contents at current cursor location
self.chords.paste(self)
                    
// Restore original pasteboard contents
//lPasteBoard.items = lPasteBoardItems