Swift 3: PerformSegue WithIdentifier Not Displaying View Controller, No Errors - ios

When I execute the line below, my destination view controller is not visually presented but there are no errors in the log:
performSegue (withIdentifier: "DetailsViewController", sender: self)
Let's step back from a problem. Imagine my successful setup: ViewController is loaded and upon some user action the above line would actually open a new DetailsViewController. I tested - everything works as expected - so I know it works.
But the problem presenting DetailsViewController begins when I decide to implement some third party framework by adding a CamViewController to my ViewController and perform segue upon element tap inside CamViewController, then delegate method passing this action back to ViewController from which I would open DetailsViewController. With this architecture setup I get no errors in a log but visually nothing is opening. No DetailsViewController ever presented.
A bit more details of architecture:
ViewController presents CamViewController with line self.present(camViewController, animated: true, completion: nil)
CamViewController implements a protocol to call some method on user action
ViewController conforms to CamViewController protocol and delegate method successfully runs on user action (log shows it is working)
Calling DetailsViewController with segue identifier is executed successfully performSegue (withIdentifier: "DetailsViewController", sender: self) but DetailsViewController is never displayed on screen.
This method below prints prepare for segue Optional("DetailsViewController") which sounds correct.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("prepare for segue \(segue.identifier)")
}
I also tried adding super.performSegue - no luck

First I would change your segue code to something more like this.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "DetailsViewController"){
print("prepare for segue DetailsViewController")
} else {
print("prepare for segue other")
}
}

Related

performSegue creates two view controllers

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
self.delegate = segue.destination as? MenuViewController
print("check_1", self.delegate)
}
#IBAction func openMenu(_ sender: Any) {
performSegue(withIdentifier: "openMenu", sender: sender)
print("check_2", self.delegate)
}
My main ViewController updates values while MenuViewController displays these values. Each time ViewController values are updated, it calls self.delegate.updateValues in MenuViewController. I transition between the two ViewControllers through buttons.
My problem is that it seems like the MenuViewController displayed is a different object than the one stored in self.delegate inside ViewController. Printing the check statements:
check_1 Optional(<Menu.MenuViewController: 0x10161ca10>)
check_2 Optional(<Menu.MenuViewController: 0x10161ca10>)
check_1 Optional(<Menu.MenuViewController: 0x10161dd10>)
May I ask how do I make sure only one instance of MenuViewController is created and stored in self.delegate?
When you add a segue to a storyboard, if you hook up the segue to a specific button/IBAction, you don't need to call performSegue manually, it will be automatically called for you.
You have 2 segues executed, since both the storyboard executes the segue and then you also do it from code by calling performSegue.
performSegue should only be used when your segue isn't directly hooked up to a UI event or if you need to conditionally perform a segue - such as when you have a login button, where depending on the network response, you might execute an error or a login segue.

navigation bar disappear after adding prepare function

I am woking on an recording APP.
I tried to add navigation controller in my first recording viewcontroller which then could pass filename array to the second viewcontroller using the following function prepare:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let controller = segue.destination as? MainViewController
controller?.recordArray = recordingArray
self.present(controller!, animated: true, completion: nil)
}
However, when ran in the simulator the navigation bar disappeared in the second controller and Xcode pops out the warning
Thread1: EXC_BAD_ACCESS(code=2,address=0x7fff51edfff8)
Has anyone got any advice?
Thanks!
Don't ever call present(_:animated:completion:) inside prepare(for segue:).
prepare(for segue) is called automatically by the system just before a segue is about to happen to let you prepare the data for sending to the destination viewcontroller or do any other calculations you need to before performing the segue. A segue needs to be set up in Storyboard and it will either be called automatically or if it is a manual segue, you need to call it using perform(segue) and once you do that, the system will call prepare(for segue) for you. You can see why calling another navigation function proves to be problematic, since you are trying to navigate to another viewcontroller using two different methods (segue and present).
If you haven't set up the segue in Storyboard, then you also need to do that, since if it is not set up, let controller = segue.destination as? MainViewController will be nil.
Once you set up the segue in Storyboard, this is how your function should look like:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let controller = segue.destination as? MainViewController {
controller.recordArray = recordingArray
self.present(controller, animated: true, completion: nil)
}
}
You shouldn’t be trying to present a VC in this method, it’s just a place for you to configure the destination VC before the segue presents it
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let controller = segue.destination as? MainViewController else { return }
controller.recordArray = recordingArray
}

check the segue used to open viewController in the viewController itself

Having this methods:
//Metodo che verifica se proveniamo dal Profile controller o da un altro controller
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "accountSegue" {
self.DisattivaTutorialBtn.isHidden=true
IndietroBtn.isHidden=false
}else{
self.IndietroBtn.isHidden=true
self.DisattivaTutorialBtn.isHidden=false
}
}
I perform different actions basing on the segue's identifier.
Of course, this method isn't called anywhere (I understood that it is): how can I call it? It seems stupid but calling it as "self" method in "viewDidLoad" doesn't work.
To do that instead of performing your Segue on a button click in storyboard, create a segue from your viewcontroller to your other view controller. then when you want to go to the next view controller call below from your button click or any other action event.
self.performSegue(withIdentifier: "accountSegue", sender: self)
This will automatically call your prepareForSegue method and your if statement will execute.
If you are using manual segues in your Storyboard, you can perform the segue by calling self.performSegue(withIdentifier:sender:). See performSegue documentation.
If you are using segues whose type is not set to manual, when you click on the UI element to which the segue is linked in your Storyboard, prepare(for segue) will be called automatically by the system.
To perform Segue manually you need to attach the action
Following will help you go get the required thing done
performSegue(withIdentifier: "accountSegue", sender: nil)

prepare(for segue: UIStoryboardSegue, sender: Any?) Not firing in Xcode 8.1 swift 3 ios 10

I have a tabbed view controller that is associated with two view controllers.
I need to pass an array from the first View Controller to the Second View controller.
In Order to do this I have the following code in my first VC:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "AddItem"
{
if let destinationVC = segue.destination as? SecondViewController
{
destinationVC.toDoList = toDoList
}
}
}
However this is not getting fired when I switch two the second VC using the tab button.
Any ideas as to why this is not getting fired?
This is how my main storyboard looks like:
Main Storyboard
You cannot transfer objects between tab views through segue because the VC is not actually making a direct segue connection rather you can do it with a delegate or a notificationCenter post and observe to transfer
You have to put that segue code in Tabbar controller class. You shouldn't put that thing in firstVC. Because there is no segue is going on from first VC to Second VC.

Segue won't execute Swift

I have a segue running form a Collection View (wrapped in a view controller) in to a another view controller, how ever the function never gets called:
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
println(segue.identifier)
println(sender)
println("SEGUE SELECTED: \(segue.identifier)")
if(segue.identifier == "segueToDetailView") {
let cell = sender as CollectionViewCell;
}
}
Have placed a breakpoint at start of function but never reached.
Any input appreciated.
I will suggest not to create segue(s) from cell or any object(like button).
Create segue from one ViewController to OtherViewController with unique identifier.
And then call the performSegueWithIdentifier yourself using the identifier.

Resources