How to Initiating a prepareForSegue function on saveAction - ios

Technologies: Swift, iOS8, XCode 6
I have a function like this;
override func prepareForSegue(segue: UIStoryboardSegue,
sender: AnyObject?)
where my identifier is
if segue.identifier == "showDetail"
I want to initiate that function on saveAction. I was working with peformSegueWithIdentifier but it keeps failing. I just want to be able to call/initiate that function once I perform my saveAction. My saveAction appears in a pop-up so I'm not sure if that is causing issues.
--- UPDATE
So, I can get it to segue to the detailview after save by doing something like this
func saveAction() {
if saved == true {
performSegueWithIdentifier("showDetail", sender: self)
}
}
But, the code I have in my prepareForSegue function, doesn't seem to get hit. Does this get bypassed? I'd like the prepareForSegue function to still implement its changes.
override func prepareForSegue(segue: UIStoryboardSegue,
sender: AnyObject?) {
if segue.identifier == "showDetail" {
do stuff here, like make the title of the ShowDetail view blue, etc..
}

Related

How to perform an action after performSegue is finished in Swift

Is there any possible way to perform an action after a segue has finished in swift?
I am calling this code:
self.performSegue(withIdentifier: "showDamageReportSegue", sender: self)
The segue opens a new ViewController modal and after the user presses the done button I want it to continue running the code underneath which is
self.performReservationEndingSequence() // which does some calls etc
Is there maybe a completion handler for performSegue that I am missing? I've been searching but I cannot find this specific question.
A reasonable solution is a callback closure.
In the showDamageReportSegue controller add a callback property
var callback : (() -> Void)?
In the showDamageReportSegue controller call callback in viewDidDisappear
override func viewDidDisappear(_ animated : Bool) {
super.viewDidDisappear(animated)
callback?()
}
or at any other arbitrary place.
In the first view controller implement prepare(for and set the callback
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "showDamageReportSegue" else { return }
let destination = segue.destination as! ShowDamageReportController // change that to the real class
destination.callback = {
self.performReservationEndingSequence()
}
}
Write it in viewDidDisappear event of the view controller.
override func viewDidDisappear(_ animated: Bool) {
super.viewDidAppear(animated)
print("ViewController Dismissed")
}
Edit: For some code to execute over a specific segue, use prepare delegate method with the triggering segue's identifier and it should execute whenever that segue is triggered:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SomeViewController" {
print("Segue triggered for SomeViewController")
}
}
There is a prepareForSegue method which is called before performSegue, you can call your function there. and also check if segue.identifier == "yourIdentifier" , then only call the function, else dont
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourSegueIdentifier" {
self.performReservationEndingSequence() // call the function
} else {
print("dont call the function")
}
}
You can try swift's method :
viewDidDisappear()

Use of unresolved identifier 'segue'

I'm trying to export my data from a ViewController to another with this code:
func prepare(for: UIStoryboardSegue, sender: AnyObject?){
let desty : EndViewController = segue.destinationViewController as! EndViewController
desty.totalScore = scoreTotal.text!
}
But when I try to do that is turns me the error:
[Use of unresolved identifier 'segue']
But I've already done it as you can see here:image
So how can I solve it.
I will be so grateful if you help me.
Please, help a young programmer to learn.
First, perform your segues as below:
self.performSegue(withIdentifier: "YourSegueID", sender: self)
Now, call prepareForSegue and change the values in destination accordingly:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "YourSegueID" {
let vc = segue.destination
vc.yourVariable = "Some Value"
}
}
You are using the wrong function. It should be...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue" {
}
}
The internal parameter label segue is missing in the first parameter and the type of the second parameter is wrong.
Please use code completion
Comment out your entire method
// func prepare(for: UIStoryboardSegue, sender: AnyObject?){
// let desty : EndViewController = segue.destinationViewController as! EndViewController
// desty.totalScore = scoreTotal.text!
// }
Type prep, you will see
Choose the proper line and press return.
Copy the body of the former method into the new one.

Swift segue is segueing to the first controller when the identifier is specified

Thanks for your time. I have a fully functional application there is this one segue that when selected it segues to the starting controller. This happens only when the app is installed and its the first time calling this segue. it doesnt happen if you close and open the app and it never happens again after that.
Scenario:
Login Successful (Segues to TabController)
Then on the 3rd Tab I have cells and when the cell is clicked it segues back to the LoginController (Should Segue to DiscoverController)
This is done with a self.performSegue(withIdentifier: "segDiscover", sender: nil)
The discoverController is never hit
The LoginController override func viewWillAppear(_ animated: Bool) is fired
I use the method override func prepare(for segue: UIStoryboardSegue, sender: Any?)
I have debugged in the simulator and the methods fire and follow through right until it leaves the controller and hits the LoginController
Any assistance would be greatly appreciated. The code below is the override func prepare(for segue: UIStoryboardSegue, sender: Any?)
Code Snip-it
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segDiscover"{
if(self._refresh.isRefreshing == false){
searchBar.text = ""
searchBarHeight.constant = 0
self.searchBarCancelButtonClicked(searchBar)
self.view.endEditing(true)
let Controller = segue.destination as! DiscoverVC
}
}
}
Thanks for your time

prepare for Segue is not being called

I have read a dozen posts on SO. Some of them suggest using Override, but I get a compile error as that function definition is now open.
self.user = user
performSegueWithIdentifier("selectPayment", sender: nil)
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destinationViewController as! SelectPaymentViewController
vc.user = user
}
that bottom function is never called. Why is this? Targeting iOS 9.2
This was just an issue with the calling syntax.
the correct implementation is
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

prepareForSegue is never called in UICollectionView

Good afternoon,
I'm trying to perform a segue from my CollectionView but the prepareForSegue is never triggered when touching a Cell. This is working for my TableView but it's not for my CollectionView.
As you can see in my code, I have both in the same ViewController, but I don't know why it's only working for the TableView. The "segueCollection" is never triggered.
What I have to do in order to make this work? There is some type of problem because I'm also using the TableView segue? I'm a little bit lost here.
Here is my code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showMovieFromTable", let destination = segue.destinationViewController as? MovieViewController, index = tableView.indexPathForSelectedRow?.row {
destination.year = searchMovies[index].year
} else if segue.identifier == "showMovieFromCollection" {
print("segueCollection")
}
print("segueTriggered")
}
I'm also trying inside this code (but it's also not showing anything):
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print(indexPath)
self.performSegueWithIdentifier("showMovieFromCollection", sender: self)
}
Thanks in advance,
Regards.
I had the same problem and got stuck there for a bit time. Finally I realize, instead of using prepareForSegue, I have to use prepare function.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "YourIdentifier" {
// stuff you want to do
}
}

Resources