Adding ChildViewControllers Using Storyboard Not Working - ios

I have a Storyboard which is shown below in the screenshot:
From my HomeController I segue to ContainerViewController using the code below. For some reason the childControllers is always nil.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let indexPath = self.tableView.indexPathForSelectedRow else {
fatalError("No row selected")
}
let place = self.places[indexPath.row]
let containerViewController = segue.destination as! ContainerViewController
containerViewController.childViewControllers // THIS IS NIL WHY!!!

Related

VC presenter is not being initialized prepare for segue IOS swift

Hello i am facing the problem as the prepare for segue function is not working properly i've the code can you please tell me how can i debug it as it has optional conditions how i can remove them and check what's the problem or what i am doing wrong as i just want a class to be initialized in target vc before the app navigates to other screen.
#IBAction func filterBtnPressed(_ sender: Any) {
self.performSegue(withIdentifier: "FilterSessionVC", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "FilterSessionVC", let navigation = segue.destination as? UINavigationController, let vc = navigation.topViewController as? FilterSessionVC {
vc.presenter = FilterSessionPresenterImplementation()
}
}
Basic debugging...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let identifier = segue.identifier
print("identifier:", identifier)
let dest = segue.destination
print("destination:", dest)
if let navigation = dest as? UINavigationController {
print("dest IS a UINavigationController")
if let vc = navigation.topViewController as? FilterSessionVC {
print("Top Controller IS a FilterSessionVC")
// here you can proceed
vc.presenter = FilterSessionPresenterImplementation()
} else {
print("Top Controller IS NOT a FilterSessionVC")
}
} else {
print("dest IS NOT a UINavigationController")
}
}
Set a breakpoint on the first line, then step through and inspect the results.

pass data from view controller to tabbarviewcontroller

I am segueing from a view controller to a tabbarcontroller and I want to pass data. I want to pass "sendAuthor" to the first view controller of the tabbarcontroller.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "FirstVC" {
let vc = (segue.destination as? UITabBarController)?.viewControllers?.first as? CommentProfileViewController
vc?.sendAuthor = sendAuthor!
vc?.Username.text = sendAuthor!
print(sendAuthor!)
}
}
In order to make sure I am getting the right thing, I included a print statement print(sendAuthor!) and I am getting the correct print statement. I also included a print statement in the destination view controller but the print statement returns nil
var sendAuthor: String?
override func viewDidLoad() {
super.viewDidLoad()
print(self.sendAuthor)
}
Your VC is imbedded in a UINavigationController. You need to take that into account as well:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "FirstVC" {
guard let tabbar = segue.destination as? UITabBarController,
let navcon = tabbar.viewControllers?.first as? UINavigationController,
let vc = navcon.topViewController as? CommentProfileViewController
else { return }
vc.sendAuthor = sendAuthor!
vc.Username.text = sendAuthor!
print(sendAuthor!)
}
}

can't set prepare for segue in iOS

I have two controllers with UINavigationController (UIViewController(root) and UITableViewController). I need to click action at UIViewController and print something when move to UITableViewController
TableViewController.swift
func printString() {
print("hello world")
}
ViewController.swift
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "sendData" {
let navController = segue.destinationViewController as! UINavigationController
let controller = navController.topViewController as! TableViewController
controller.printString()
}
}
And after click I see that
Could not cast value of type 'myProjectName.TableViewController' (0x5a068) to 'UINavigationController' (0x17ffed4).
Why is that happening?
What about just:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "sendData",
let controller = segue.destinationViewController as? TableViewController {
controller.printString()
}
}
}
It looks like the destinationViewController is the TableViewController.

Passing object with prepareForSegue Swift

I am trying to pass an object to another scene with prepareForSegue()
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
var nextScene = segue.destinationViewController as! VehicleDetailsTableViewController
// Pass the selected object to the new view controller.
if let indexPath = self.tableView.indexPathForSelectedRow() {
let selectedVehicle = vehicles[indexPath.row]
nextScene.currentVehicle = selectedVehicle
}
}
And I have currentVehicle object to catch these object. But, when I try to run, it brokes and get error about downcasting.
Error EDIT
Could not cast value of type 'XXX.DisplayViewController'
(0x1082dcd80) to 'XXX.VehicleDetailsTableViewController'
(0x1082dc9a0). (lldb)
You have to give the segue an identifier in the storyboard.(say mySegue)
Using Xcode 10 swift 4.x(Also works with Xcode 9 & 8 , swift 3.x)
override func prepare(for segue: UIStoryboardSegue, sender: Any?){}
Is called for all segues being called from your current UIViewController. So the identifier is to differentiate the different segues
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mySegue" ,
let nextScene = segue.destination as? VehicleDetailsTableViewController ,
let indexPath = self.tableView.indexPathForSelectedRow {
let selectedVehicle = vehicles[indexPath.row]
nextScene.currentVehicle = selectedVehicle
}
}
If you are using Using Xcode 7, swift 2.x
Then use this code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "mySegue" {
var nextScene = segue.destinationViewController as! VehicleDetailsTableViewController
// Pass the selected object to the new view controller.
if let indexPath = self.tableView.indexPathForSelectedRow {
let selectedVehicle = vehicles[indexPath.row]
nextScene.currentVehicle = selectedVehicle
}
}
}
Place a breakpoint after nextScene and see if it is being triggered by clicking any cell in the TableView. If it isn't then the identifier name u provided in the storyboard must be different then the one given here.
In swift 3.0 Xcode 8 beta 6
AnyObject has been renamed to Any?
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
let nextScene = segue.destinationViewController as! VehicleDetailsTableViewController
// Pass the selected object to the new view controller.
if let indexPath = self.tableView.indexPathForSelectedRow {
let selectedVehicle = vehicles[indexPath.row]
nextScene.currentVehicle = selectedVehicle
}
}
}
In Swift 3, Xcode 8
Just don't use "identifier". I don't know why but "segue.identifier" is always nil. I just used prepare method like this and works!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let secondController = segue.destination as? SecondController {
secondController.anyItemInSecondController = self.anyItemInFirstController
}
What that error is telling you is that segue.destinationViewController isn't a VehicleDetailsTableViewController. We don't have enough details to tell you why.
Check your segues and make sure they're all pointing to the correct place, and always check the identifier of your segue before you perform a cast.
if segue.identifier == "theNameOfYourSegue" // then do your cast

Swift pass data between VCs

i'm new to Swift and i would like to know why this piece of code i translated from Obj-C doesn't work here.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "scoreDetail" {
let indexPath = self.tableView.indexPathForSelectedRow()
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
let itemVC: ScoreVC = segue.destinationViewController as ScoreVC
itemVC.detailItem = object
}
}
Here is the storyboard. I have a Show Replace segue from the cell to the UINavigatorController
I get this error as soon as i click on a UITableViewCell
The problem has nothing to do with Swift. The destinationViewController is the navigation controller, not your ScoreVC, so the code you translated that from shouldn't have worked either (if it had the same setup). It should look like this,
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "scoreDetail" {
let indexPath = self.tableView.indexPathForSelectedRow()
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
let nav : UINavigationController = segue.destinationViewController
let itemVC: ScoreVC = nav.topViewController as ScoreVC
itemVC.detailItem = object
}
}

Resources