Reputation: 11
import UIKit
import CoreData
class KontakteTableViewController: UITableViewController {
@IBOutlet var KontakteTableView: UITableView!
var kontakteArray = [Kontakt]()
var selectedKontakt: Kontakt?
override func viewDidLoad() {
super.viewDidLoad()
KontakteTableView.delegate = self
KontakteTableView.dataSource = self
loadData()
}
@IBAction func addKontaktButton(_ sender: UIBarButtonItem) {
}
@IBAction func searchKontaktButton(_ sender: UIBarButtonItem) {
}
@IBAction func deleteAllButton(_ sender: UIBarButtonItem) {
}
func createKontakt() {
let alert = UIAlertController(title: "Add contact", message: nil, preferredStyle: .alert
)
alert.addTextField { (textField) in
textField.placeholder = "Vorname"
}
alert.addTextField { (textField) in
textField.placeholder = "Nachname"
}
alert.addTextField { (textField) in
textField.placeholder = "Telefonnummer"
textField.keyboardType = .numberPad
}
let action = UIAlertAction(title: "OK", style: .default) { (_) in
if alert.textFields?[0].text?.count != 0 && alert.textFields?[1].text?.count != 0 && alert.textFields?[2].text?.count != 0 {
let vorname = alert.textFields?[0].text
let nachname = alert.textFields?[1].text
let telefonnummer = Int64((alert.textFields?[2].text)!)
// Core Data
let kontakt = CoreDataService.defaults.createKontakt(_vorname: vorname!, _nachname: nachname!, _telefonnummer: telefonnummer!)
// Array
self.kontakteArray.append(kontakt)
self.KontakteTableView.reloadData()
} else {
self.errorMessage(_message: "Bitte Daten angeben")
}
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
func loadData() {
let kontakteArray = CoreDataService.defaults.loadData()
if let _kontakteArray = kontakteArray {
self.kontakteArray = _kontakteArray
self.KontakteTableView.reloadData()
}
}
func errorMessage(_message: String) {
let alert = UIAlertController(title: "Fehler", message: _message, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default) { (_) in }
alert.addAction(okAction)
present(alert, animated: true, completion: nil)
}
@objc func longPress(_ sender: UIGestureRecognizer) {
if sender.state == UIGestureRecognizer.State.ended {
let longPressLocationPoint = sender.location(in: self.KontakteTableView)
if let pressIndexPath = self.KontakteTableView.indexPathForRow(at: longPressLocationPoint) {
var task = UITextField()
let alert = UIAlertController(title: "Änderung", message: "Neue Daten eingeben", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default) { (action) in
self.kontakteArray[pressIndexPath.row].telefonnummer = Int64(task.text!)!
self.KontakteTableView.reloadData()
CoreDataService.defaults.saveContext()
}
alert.addTextField { (textField) in
textField.placeholder = "Neue Telefonnummer"
textField.keyboardType = .numberPad
task = textField
}
let cancelAction = UIAlertAction(title: "Abbrechen", style: .default) { (_) in
}
alert.addAction(action)
alert.addAction(cancelAction)
present(alert, animated: true, completion: nil)
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "GoToKontakteInfo" {
let destVC = segue.destination as! KontaktInfosViewController
destVC.kontakt = selectedKontakt
}
}
}
extension KontakteTableViewController {
// MARK: - Table View DataSource
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return kontakteArray.count
} // Zeile erstellen
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:)))
cell.addGestureRecognizer(longPressRecognizer)
let kontakt = kontakteArray[indexPath.row]
cell.textLabel?.text = "Name: \(kontakt.vorname!) Alter: \(kontakt.nachname!) Telefonnummer: \(kontakt.telefonnummer)"
return cell
}
}
extension KontakteTableViewController {
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
CoreDataService.defaults.deleteKontaktFromDataStack(indexPath: indexPath, kontakteArray: &kontakteArray)
KontakteTableView.reloadData()
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedKontakt = kontakteArray[indexPath.row]
performSegue(withIdentifier: "GoToUserInformationSeque", sender: nil)
KontakteTableView.deselectRow(at: indexPath, animated: true)
}
}
Upvotes: 1
Views: 78
Reputation: 326
The problem may be in your storyboard. Select your first screen that you want to be presented and in the attributes inspector, check "Initial View Controller". Sometimes when I change around the storyboard this happens to me.
Upvotes: 1