Skip to content

Commit fd36831

Browse files
committed
fix update action
1 parent 2ec0dbd commit fd36831

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed
-9 Bytes
Binary file not shown.

ListaCoreData/ViewController.swift

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,33 @@ class ViewController: ListaCoreDataFunctions, UITableViewDataSource {
9393

9494
var alerta = UIAlertController(title: "Editando item", message: "Altere o nome do item", preferredStyle: .Alert)
9595
let botaoSalvar = UIAlertAction(title: "Salvar", style: .Default) { (action) in
96-
let novoNome = alerta.textFields![0] as! UITextField
96+
let novoNome = (alerta.textFields![0] as! UITextField).text
9797

98-
// Chama a função para atualizar o nome
99-
self.atualizarItem(nomeAtual, novoNome: novoNome.text)
98+
// Verifica se o nome está em branco
99+
if (novoNome.isEmpty) {
100+
self.alertaError("Erro ao salvar item", msg: "O nome do item não pode ficar em branco")
101+
}
102+
// Se o nome atual for igual o nome anterior, não faz nada
103+
else if (nomeAtual == novoNome) {
104+
true
105+
}
106+
// Verifica se o novo nome já existe
107+
else if (self.itemExistente(novoNome)) {
108+
self.alertaError("Erro ao salvar item", msg: "Já existe um item com o nome: \(novoNome) \n Não é possível salvar dois nomes iguais")
109+
}
110+
// Salva no CoreData
111+
else {
112+
// Chama a função para atualizar o nome
113+
self.atualizarItem(nomeAtual, novoNome: novoNome)
114+
115+
// Racarrega os dados no CoreData
116+
self.carregaDadosCoreData()
117+
118+
// Recarrega a TableView
119+
self.tableView.reloadData()
120+
}
100121

101-
// Racarrega os dados no CoreData
102-
self.carregaDadosCoreData()
103-
104-
// Recarrega a TableView
105-
self.tableView.reloadData()
122+
self.tableView.setEditing(false, animated: false)
106123
}
107124

108125
// Essa função é necessária para o textField aparecer no alerta

0 commit comments

Comments
 (0)