I'm starting working with Xcode and Swift and I'm now stuck. I did as I learned doing the Apple tutorial on the net, but my problem is that I can't get back to my home page once I saved an item.
I have two views with their controllers: my homepage and my new item page. I have a navigation controller between them, and I try to go from homepage to newItem page with a button (not a buttonbar). This is working.
I have my segue setup and my prepare function into my newItem page, but once I filled all the required fields, my Save button is enable, but nothing happen when I click on it, and nothing is written as output to help me.
Here are my prepare and unwind function (into homePage):
//MARK: Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
switch(segue.identifier ?? "") {
case "AddLending":
os_log("Adding a new lending.", log: OSLog.default, type: .debug)
case "ShowItems":
guard segue.destination is NewLendingViewController else {
fatalError("Unexpected destination: \(segue.destination)")
}
/*guard let selectedMealCell = sender as? MealTableViewCell else {
fatalError("Unexpected sender: \(sender)")
}
guard let indexPath = tableView.indexPath(for: selectedMealCell) else {
fatalError("The selected cell is not being displayed by the table")
}
let selectedMeal = meals[indexPath.row]
mealDetailViewController.meal = selectedMeal*/
default:
fatalError("Unexpected segue identifier: \(segue.identifier)")
}
}
//MARK: Actions
// This function is dealing with calling back the homePage once a lending has been created
@IBAction func unwindToHomePage(sender: UIStoryboardSegue) {
if let sourceViewController = sender.source as? NewLendingViewController, let lending = sourceViewController.lending {
money += lending.amount
updateMoneyButton()
// Save tne lendings.
saveLendings()
}
}
And here is my prepare function of my newItem page:
// MARK: - Navigation
@IBAction func lendingCancelButton(_ sender: UIBarButtonItem) {
// Depending on style of presentation (modal or push presentation), this view controller needs to be dismissed in two different ways
let isPresentingInAddLendingMode = presentingViewController is UINavigationController
if isPresentingInAddLendingMode {
dismiss(animated: true, completion: nil)
} else if let owningNavigationController = navigationController {
owningNavigationController.popViewController(animated: true)
} else {
fatalError("the NewLendingViewController is not inside a navigation controller.")
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
// Configure the destination view controller only when the saved button is pressed.
guard let button = sender as? UIBarButtonItem, button === lendingSaveButton else {
os_log("The saved button was not pressed, cancelling", log: OSLog.default, type: .debug)
return
}
let title = lendingTitle.text ?? ""
guard let amount = Int(lendingAmount.text ?? "0") else {
os_log("It seems that the amount entered is incorrect", log: OSLog.default, type: .debug)
return
}
let contact = lendingContact.text ?? ""
//let date = lendingDate.text ?? ""
let date = formatStringToDate(date: lendingDate.text ?? "")
// Set the lending to be passed to HomePageViewController after the unwind segue.
lending = Lending(amount: amount, title: title, contact: contact, lendingDate: date)
}
Just to tell, my Cancel button is working fine and correctly leading me back to my homepage.