Swift - Can't get back to my homepage once I clicked on Save button
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.
ios swift
add a comment |
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.
ios swift
Is it just me, or is your "unwindToHomepage" function not actually doing any navigation? It seems to just update themoney
amount and then callsaveLendings
. Where is the navigation supposed to be happening here? ThelendingCancelButton
is actually taking care of navigation, so I guess that's why it's working there. Maybe I've missed something?
– CPR
Nov 23 '18 at 16:57
2
Take a look at this https://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them
– MwcsMac
Nov 23 '18 at 18:47
Hi MwcsMac, this link solved my problem, in fact my code is right, but I was missing to link my Save button with the exit part of the view in the Storyboard. You can detail this and I could mark it as solved.
– Alexandre
Nov 25 '18 at 21:57
add a comment |
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.
ios swift
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.
ios swift
ios swift
edited Nov 24 '18 at 13:23
wvteijlingen
8,52612544
8,52612544
asked Nov 23 '18 at 16:46
AlexandreAlexandre
5982818
5982818
Is it just me, or is your "unwindToHomepage" function not actually doing any navigation? It seems to just update themoney
amount and then callsaveLendings
. Where is the navigation supposed to be happening here? ThelendingCancelButton
is actually taking care of navigation, so I guess that's why it's working there. Maybe I've missed something?
– CPR
Nov 23 '18 at 16:57
2
Take a look at this https://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them
– MwcsMac
Nov 23 '18 at 18:47
Hi MwcsMac, this link solved my problem, in fact my code is right, but I was missing to link my Save button with the exit part of the view in the Storyboard. You can detail this and I could mark it as solved.
– Alexandre
Nov 25 '18 at 21:57
add a comment |
Is it just me, or is your "unwindToHomepage" function not actually doing any navigation? It seems to just update themoney
amount and then callsaveLendings
. Where is the navigation supposed to be happening here? ThelendingCancelButton
is actually taking care of navigation, so I guess that's why it's working there. Maybe I've missed something?
– CPR
Nov 23 '18 at 16:57
2
Take a look at this https://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them
– MwcsMac
Nov 23 '18 at 18:47
Hi MwcsMac, this link solved my problem, in fact my code is right, but I was missing to link my Save button with the exit part of the view in the Storyboard. You can detail this and I could mark it as solved.
– Alexandre
Nov 25 '18 at 21:57
Is it just me, or is your "unwindToHomepage" function not actually doing any navigation? It seems to just update the
money
amount and then call saveLendings
. Where is the navigation supposed to be happening here? The lendingCancelButton
is actually taking care of navigation, so I guess that's why it's working there. Maybe I've missed something?– CPR
Nov 23 '18 at 16:57
Is it just me, or is your "unwindToHomepage" function not actually doing any navigation? It seems to just update the
money
amount and then call saveLendings
. Where is the navigation supposed to be happening here? The lendingCancelButton
is actually taking care of navigation, so I guess that's why it's working there. Maybe I've missed something?– CPR
Nov 23 '18 at 16:57
2
2
Take a look at this https://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them
– MwcsMac
Nov 23 '18 at 18:47
Take a look at this https://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them
– MwcsMac
Nov 23 '18 at 18:47
Hi MwcsMac, this link solved my problem, in fact my code is right, but I was missing to link my Save button with the exit part of the view in the Storyboard. You can detail this and I could mark it as solved.
– Alexandre
Nov 25 '18 at 21:57
Hi MwcsMac, this link solved my problem, in fact my code is right, but I was missing to link my Save button with the exit part of the view in the Storyboard. You can detail this and I could mark it as solved.
– Alexandre
Nov 25 '18 at 21:57
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450357%2fswift-cant-get-back-to-my-homepage-once-i-clicked-on-save-button%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450357%2fswift-cant-get-back-to-my-homepage-once-i-clicked-on-save-button%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is it just me, or is your "unwindToHomepage" function not actually doing any navigation? It seems to just update the
money
amount and then callsaveLendings
. Where is the navigation supposed to be happening here? ThelendingCancelButton
is actually taking care of navigation, so I guess that's why it's working there. Maybe I've missed something?– CPR
Nov 23 '18 at 16:57
2
Take a look at this https://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them
– MwcsMac
Nov 23 '18 at 18:47
Hi MwcsMac, this link solved my problem, in fact my code is right, but I was missing to link my Save button with the exit part of the view in the Storyboard. You can detail this and I could mark it as solved.
– Alexandre
Nov 25 '18 at 21:57