我开始使用Xcode和Swift,但现在陷入困境。我在网上学习Apple教程的过程中确实做过,但是我的问题是,保存项目后就无法回到主页。
我对他们的控制器有两个视图:我的主页和新项目页面。 我之间有一个导航控制器,我尝试使用按钮(而不是按钮栏)从主页转到newItem页面。工作正常。
我在我的newItem页面中设置了segue设置和我的prepare函数,但是一旦我填写了所有必填字段,我的Save按钮就启用了,但是当我单击它时什么也没有发生,并且没有任何内容被写为输出来帮助我
这是我的准备和展开功能(进入主页):
//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()
}
}
这是我的newItem页面的prepare函数:
// 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)
}
仅需说明一下,我的“取消”按钮可以正常工作,并且可以正确地将我带回到首页。