pass data from view controller to tabbarviewcontroller - ios

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!)
}
}

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.

Passing data from a JSON array to another view controller

I'm trying to pass data from one view controller such as the name and trying to display it in the next view controller but I'm not sure how to do it. I'm using this API which is a JSON array to get the data for my app. I've looked all over for the answer to this but everything I try doesn't work. This is the code I'm using to try it right now but it doesn't work, this is in the first view controller
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {`
if segue.identifier == "nextView" {
if let name = sender as? String, let nextView = segue.destination as? ListViewController {
nextView.nameText = name
}
}
}
This is the code in the destination view controller
class ListViewController: UIViewController {
#IBOutlet weak var name: UILabel!
var nameText: String = ""
override func viewDidLoad() {
super.viewDidLoad()
name?.text = nameText
}
}
The var you are trying to set in prepare nameText is a String so you should send it a String and not a [jsonList]
try like this :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "view" {
if let name = sender as? [jsonList], let nextView = segue.destination as! ListViewController {
nextView.nameText = //change affectation here
}
}
}

Adding ChildViewControllers Using Storyboard Not Working

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!!!

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.

Using prepareForSegue for table view controller

I have a table view controller. I have to set a value inside this controller. But I guess above code works only for view controller.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier! == "changeName" {
if let destination = segue.destinationViewController as? NameSettingTableViewController {
println("ok")
}
}
}
It doesn't print "ok" to debug. I think because NameSettingTableViewController is not a view controller. destination variable is returning nil. How can I fix this ?
Storyboard:http://prntscr.com/79846c
You need to get the navigation controller first, then access its topViewController property to get a reference to your NameSettingTableViewController:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier! == "changeName" {
if let destination = segue.destinationViewController as? UINavigationController {
let destVC = destination.topViewController as! NameSettingTableViewController
// set whatever variables on destVC
println("ok")
}
}
}

Resources