本文介绍了Swift OSX NSImageView拖放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将图像拖放到NSImageView
上之后执行操作,但是它不起作用.如何控制拖放操作?
I am trying to execute an action after dropping an image on a drag and drop NSImageView
but it is not working. How can I control the drag and drop operations?
我有一个logoFornecedorImageView
,它是一个NSImageView
插座.我的课程从NSDraggingDestinatio
继承,并且拖动的类型已注册,但是当我运行该软件并在其上拖动图像时,没有任何反应,控制台上也没有打印任何内容.
I have the logoFornecedorImageView
that is an NSImageView
outlet. My class inherits from NSDraggingDestinatio
and the dragged types are registered, but when I run the software and drag an image on it nothing happens, nothing is printed in the console.
import Cocoa
class InserirFornecedorViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate, NSDraggingDestination {
@IBOutlet weak var tituloJanelaLabel: NSTextField!
@IBOutlet weak var logoFornecedorImageView: NSImageView!
@IBOutlet weak var nomeFornecedorTextField: NSTextField!
@IBOutlet weak var materialFornecidoTextField: NSTextField!
@IBOutlet weak var materiaisTableView: NSTableView!
@IBOutlet weak var indicadorAtividadeProgressIndicator: NSProgressIndicator!
@IBOutlet weak var salvarFornercedorButton: NSButton!
var fornecedor: Fornecedor?
var logoFornecedorSelecionada = false
override func viewDidLoad() {
super.viewDidLoad()
materiaisTableView.dataSource = self
materiaisTableView.delegate = self
logoFornecedorImageView.register(forDraggedTypes: logoFornecedorImageView.registeredDraggedTypes)
fornecedor = Fornecedor()
}
func draggingEnded(_ sender: NSDraggingInfo?) {
print("END")
logoFornecedorSelecionada = true
}
func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
print("ENTERED")
return .generic
}
func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation {
print("UPDATED")
return .generic
}
func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
return true
}
func numberOfRows(in tableView: NSTableView) -> Int {
return fornecedor?.materiais.count ?? 0
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
var cell: NSTableCellView?
//if tableColumn == tableView.tableColumns[0]
if fornecedor?.materiais.count != 0 {
let identificadorCell = "materialCellView"
let material = fornecedor?.materiais[row]
cell = tableView.make(withIdentifier: identificadorCell, owner: nil) as? NSTableCellView
cell?.textField?.stringValue = material!
}
return cell
}
@IBAction func selecionarImagemButtonClicked(_ sender: NSButton) {
let panel = NSOpenPanel()
panel.canChooseFiles = true
panel.canChooseDirectories = false
panel.allowsMultipleSelection = false
panel.canCreateDirectories = false
//panel.allowedFileTypes = ["jpg","png","pct","bmp", "tiff"]
panel.allowedFileTypes = NSImage.imageTypes()
panel.beginSheetModal(for: view.window!) { (result) in
if result == NSFileHandlingPanelOKButton {
self.logoFornecedorImageView.image = NSImage(byReferencing: panel.url!)
self.logoFornecedorSelecionada = true
}
}
}
@IBAction func removerImagemButtonClicked(_ sender: NSButton) {
logoFornecedorImageView.image = NSImage(named: "LogoImagemTexto")
logoFornecedorSelecionada = false
}
@IBAction func adicionarMaterialButton(_ sender: NSButton) {
if materialFornecidoTextField.stringValue.isEmpty {
mostrarErro(mensagem: "Erro de preenchimento", informativo: "Informe o material")
materialFornecidoTextField.becomeFirstResponder()
} else {
fornecedor?.materiais.append(materialFornecidoTextField.stringValue)
materialFornecidoTextField.stringValue = ""
fornecedor?.materiais.sort {
$0.localizedCaseInsensitiveCompare($1) == ComparisonResult.orderedAscending
}
materiaisTableView.reloadData()
}
}
@IBAction func voltarButton(_ sender: NSButton) {
//let usarSoftViewController = presenting as! UsarSoftViewController
//usarSoftViewController.ativarBoxPrincipal()
//usarSoftViewController.usuario = usuario
//usarSoftViewController.fazerLogin()
dismiss(self)
}
func mostrarErro(mensagem: String, informativo: String) {
let alert = NSAlert()
alert.messageText = mensagem
alert.informativeText = informativo
alert.addButton(withTitle: "Fechar")
alert.alertStyle = .critical
alert.runModal()
}
}
推荐答案
感谢大家
我刚刚执行了一个操作,效果很好.
I just performed an action and it worked fine.
@IBAction func logoFornecedorImageDropped(_ sender: NSImageView) {
self.logoFornecedorSelecionada = true
}
这篇关于Swift OSX NSImageView拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!