Я создал viewController с предупреждением и действием, называемым saveAction. когда я нажимаю saveActionButton в предупреждении, он должен вставить строку в массив (который хранится в другом классе), а представление таблицы в следующем viewController должно перезагрузить данные и количество ячеек. Я попытался объявить tableView вfirstViewController, но он не работает, я всегда получаю сообщение об ошибке: Поток 1: Неустранимая ошибка: неожиданно обнаружен ноль при неявном развертывании необязательного значения. у тебя есть идеи?
code in the firstViewController
var tblv: UITableView? = nil
@IBAction func addButtonPressed(_ sender: Any) {
buttonAnimation(button: addButton)
singleToneClassData.passwordString.insert("\(passwordLabel.text ?? "")", at: 0)
let alert = UIAlertController(title: "\(passwordLabel.text ?? "")", message: "customize your password", preferredStyle: .alert)
alert.addTextField { (notesTextfield) in
notesTextfield.placeholder = "type in some notes"
notesTextfield.textAlignment = .center
}
alert.addTextField { (categoryTextfield) in
categoryTextfield.placeholder = "choose your password category"
categoryTextfield.textAlignment = .center
categoryTextfield.inputView = self.picker
}
let actionSave = UIAlertAction(title: "save", style: .default) { (cancelButton) in
self.dismiss(animated: true, completion: nil)
self.singleToneClassData.passwordNote.insert("\(alert.textFields?.first?.text ?? "")", at: 0)
print("passwordArray: \(self.singleToneClassData.passwordString)")
print("passwordNotesArray: \(self.singleToneClassData.passwordNote)")
print("passwordStringArray Länge: \(self.singleToneClassData.passwordString.count)")
self.tblv?.reloadData()
// self.secondController.tableView.reloadData()
}
let actionCancel = UIAlertAction(title: "cancel", style: .default) { (saveButton) in
self.dismiss(animated: true, completion: nil)
}
alert.addAction(actionCancel)
alert.addAction(actionSave)
self.present(alert, animated: true)
}
class where i store my data
class passwordInformation {
var passwordString = [String]()
var passwordNote = [String]()
var passwordCategoryText = [String]()
}
the tablevieClass
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return singleToneClassData.passwordString.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.passwordStringLabel.text = singleToneClassData.passwordString[indexPath.row]
cell.notesTextfield.text = singleToneClassData.passwordNote[indexPath.row]
// cell.categoryLabel.text = singleToneClassData.passwordCategoryText[indexPath.row]
cell.backgroundColor = UIColor.green
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
headLineLabel.text = "my password list"
tableView.delegate = self
tableView.dataSource = self
let firstController = FirstViewController()
firstController.tblv = tableView
let nibName = UINib(nibName: "TableViewCell", bundle: nil)
tableView.register(nibName, forCellReuseIdentifier: "cell")
}