UIPasteBoard的呼叫问题

时间:2020-05-01 09:02:22

标签: ios swift uipasteboard

我的键盘有问题。

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

为此,我使用UIPasteBoard类在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
    }
}

正如我之前指定的,当我尝试写东西时,直到不复制随机内容,一切都不会发生。

1 个答案:

答案 0 :(得分:0)

我自己找到了解决方案。问题是我没有创建剪贴板。因此,要写入所选单元格,我必须将其推两次。因此,我首先清理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