に役立ちます。
私はUILabel
とUIButton
とUIViewController
を持っている例を作成しました。
ボタンを押すたびに、画面に3つのボタン(Copy
,Paste
、View Memory
)を含むアクションシートメニューが表示されます。
アクションシートのCopy
ボタンを押すと、ラベルのテキストがコピーされます。
![enter image description here](https://i.stack.imgur.com/BRqoU.png)
これはコードです:
注:displayResultLabel
という名前
UILabel
。
UIButton
のアクションは、showActionSheetButtonAction
となります。
class ViewController: UIViewController {
@IBOutlet weak var displayResultLabel: UILabel!
@IBAction func showActionSheetButtonAction(_ sender: UIButton) {
let actionSheetController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
actionSheetController.addAction(
UIAlertAction(title: "Copy", style: .default, handler: { [weak self] _ in
guard let strongSelf = self else { return }
UIPasteboard.general.string = strongSelf.displayResultLabel.text
})
)
actionSheetController.addAction(
UIAlertAction(title: "Paste", style: .default, handler: { _ in
// Where to handle when the Paste button is pressed
})
)
actionSheetController.addAction(
UIAlertAction(title: "View Memory", style: .default, handler: { _ in
// Where to handle when the View Memory button is pressed
})
)
actionSheetController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(actionSheetController, animated: true, completion: nil)
}
}
あなたはアクションシートにコピーアイコンを付けるだけですか? –
@MohammadBashirSidaniコピー機能でこのメニューを開くには、このボタンを押す必要があります。 – KVL