Swift pass data through navigation controller - ios

One of my segues transitions from a view controller to a tableview controller. I want to pass an array between the two, but with a navigation controller before the tableview controller, I can't figure out how to pass the array. How do you pass data through a navigatiom controller to a tableview controller?

Override prepareForSegue and set whatever value on the tableview you'd like. You can grab the tableview from the UINavigation viewControllers property.
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!){
let navVC = segue.destinationViewController as UINavigationController
let tableVC = navVC.viewControllers.first as YourTableViewControllerClass
tableVC.yourTableViewArray = localArrayValue
}
For Swift 3 :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let navVC = segue.destination as? UINavigationController
let tableVC = navVC?.viewControllers.first as! YourTableViewControllerClass
tableVC.yourTableViewArray = localArrayValue
}

#Tommy Devoy's answer is correct but here is the same thing in swift 3
Updated Swift 3
// 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?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "yourSegueIdentifier" {
if let navController = segue.destination as? UINavigationController {
if let chidVC = navController.topViewController as? YourViewController {
//TODO: access here chid VC like childVC.yourTableViewArray = localArrayValue
}
}
}
}

I was getting this error to Horatio's solution.
ERROR: Value of type 'UINavigationController' has no member 'yourTableViewArray'
So this might help others like me looking for a code only solution. I wanted to redirect to a Navigation controller, yet pass data to root view controller within that Nav Controller.
if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourNavigationControllerID") as? UINavigationController,
let yourViewController = controller.viewControllers.first as? YourViewController {
yourViewController.yourVariableName = "value"
self.window?.rootViewController = controller // if presented from AppDelegate
// present(controller, animated: true, completion: nil) // if presented from ViewController
}

SWIFT 3 (tested solution) - Passing data between VC - Segue with NavigationController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let navigationContoller = segue.destination as! UINavigationController
let receiverViewController = navigationContoller?.topViewController as ReceiverViewController
receiverViewController.reveivedObject = sentObject
}

Tommy's solution requires you to set up the Segue in Storyboard.
Here is a code only solution:
func functionToPassAsAction() {
var controller: UINavigationController
controller = self.storyboard?.instantiateViewControllerWithIdentifier("NavigationVCIdentifierFromStoryboard") as! UINavigationController
controller.yourTableViewArray = localArrayValue
self.presentViewController(controller, animated: true, completion: nil)
}

Swift 5
class MyNavigationController: UINavigationController {
var myData: String!
override func viewDidLoad() {
if let vc = self.topViewController as? MyViewcontoller{
vc.data = self.myData
}
}
}
let navigation = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MyNavigationController") as! MyNavigationController
navigation.myData = "Data that you want to pass"
present(navigation, animated: true, completion: nil)

FIXED syntax for SWIFT 3
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let navVC = segue.destination as! UINavigationController
let tableVC = navVC.viewControllers.first as! YourTableViewControllerClass
tableVC.yourTableViewArray = localArrayValue
}

passing a text in tableview swift 4
**or just pass data but change the function **
First View Controller
class
let name_array = ["BILL GATES", "MARK ZUKERBERG", "DULKAR SALMAN", "TOVINO" , "SMITH"]
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
let story: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let new = story.instantiateViewController(withIdentifier: "SecondViewController")as! SecondViewController
new.str1 = name_array[indexPath.row]
self.navigationController?.pushViewController(new, animated: true)
Second View Controller
class SecondViewController: UIViewController {
#IBOutlet weak var lbl2: UILabel!
var str1 = String()
override func viewDidLoad() {
super.viewDidLoad()
lbl2.text = str1
}

Related

Could not cast value of type to UINavigationController swift3

I'm getting the following cast error (when I click on the "+" button in the simulator [NavigationController]) and can't figure out why.
I'm using Swift3. Thanks for your help!
MainVC.swift
protocol AddTaskDelegate: class {
func saveTask(by controller: AddVC, with data: [String:String])
}
class MainVC: UIViewController {
// all tasks from the database
var tasks = [ToDoListTasks]()
#IBOutlet weak var table: UITableView!
// connect with Core Data
let managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
...
// prepare segue to send data
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "AddSegue" {
let navigationController = segue.destination as! UINavigationController
let childViewController = navigationController.topViewController as! AddVC
childViewController.delegate = self as! AddTaskDelegate
}
}
}
AddVC.swift
class AddVC: UIViewController {
weak var delegate: AddTaskDelegate?
...
Just read the error message. You are saying
segue.destination as! UINavigationController
But the destination of this segue is not a UINavigationController.

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

How can I pass data between viewcontrollers? (Swift) [duplicate]

I am trying to convert an app from Objective-C to Swift but I can't find how to pass data between views using Swift. My Objective-C code is
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
AnsViewController *ansViewController;
ansViewController = [storyBoard instantiateViewControllerWithIdentifier:#"ansView"];
ansViewController.num = theNum;
[self presentViewController:ansViewController animated:YES completion:nil];
What that is doing is it basically takes the variable, theNum, and passes it to the variable, num, on a different view controller. I know this may be an easy question but I am getting pretty confused with Swift so if someone could explain how they changed it to Swift that would be greatly appreciated!
Thanks
Let's assumed we stand at the firstView go to the DetailView and want passing data from firstView to Detailview. To do that with storyboard, at the firstView we will have a method:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "segueTest") {
//Checking identifier is crucial as there might be multiple
// segues attached to same view
var detailVC = segue!.destinationViewController as DetailViewController;
detailVC.toPass = textField.text
}
}
and then into the class of DetailView we declared a variable:
var toPass: String!
then you can use the variable toPass (of course you can change the type of the variable as you want, in this EX I just demo for string type).
class AnsViewController: UIViewController {
var theNum: Int
override func viewDidLoad() {
super.viewDidLoad()
println(theNum)
}
}
override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
let viewController = self.storyboard.instantiateViewControllerWithIdentifier("ansView") as AnsViewController
viewController.num = theNum
self.presentViewController(viewController, animated: true, completion: nil)
}
To pass string or any data from one controller to another in swift.
Follows below steps:
1) Create property in child controller as var abc:string!
2) Create object of childcontroller
let storyboard:UIStoryboard()
let viewController: childcontroller = storyboard.instantiateViewControllerWithIdentifier("childcontroller") as! childcontroller
viewController.abc = "hello";
self.navigationController.pushviewController(Controller:viewController animated:true CompletionHandler:nil)
Using tableview,
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let ClubProfileView = self.storyboard?.instantiateViewController(withIdentifier: "CBClubProfileViewController") as! CBClubProfileViewController
let TempCulubDic:NSDictionary =
((ClubsListbyDateDic.object(forKey:((ClubsListbyDateDic.allKeys as! [String]).sorted(by: <)as NSArray).object(at: indexPath.section) as! String) as! NSArray).object(at: indexPath.row))as! NSDictionary
let ClubId:String=(TempCulubDic.value(forKey: "club_id") as? String)!
let CheckIndate:String=(TempCulubDic.value(forKey: "chekin_date") as? String)!
ClubProfileView.ClubID=ClubId
ClubProfileView.CheckInDate = CheckIndate
// self.tabBarController?.tabBar.isHidden=true
ClubProfileView.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(ClubProfileView, animated: true)
}
#IBAction func nextbtnpreesd(_ sender: Any) {
let mystring = "2"
performSegue(withIdentifier: "MusicVC", sender: mystring)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? MusicVC{
if let song = sender as? String{
destination.strlablevalie = song
}
}
}
//in MusicVC create string like:
var strlablevalie:String!
To pass string or any data from one controller to another in swift.
Follows below steps:
1) Create variable of type which you want like (String,Int)
var test : String!
2) Create object of childcontroller
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Here identifier of your VC") as! (Here Name Of Your Controller)
vc.test = "Hello" (Or any data which you want to pass)
self.navigationController?.pushViewController(VC, animated: true)
That should solve your problem
Note: if we are using storyboard
Step 1: Master controller :
// table row which row was selected
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
println("You selected cell #\(indexPath.row)!")
nextScreenRow = indexPath.row
// get to the next screen
self.performSegueWithIdentifier("dashboard_static_screen_segue", sender: self)
}
and then;
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "dashboard_static_screen_segue") {
var detailController = segue.destinationViewController as StaticScreens;
detailController.screenNumber = nextScreenRow
}
}// end prepareForSegue
step 2: Detail controller (StaticScreen)
// set variable into your detail controller
var screenNumber: NSInteger?
println("selected row \(screenNumber!)")

Passing data with segue through navigationController

I'm trying to push data from one viewController to another. I've connected a viewController to another ViewController's navigationController and then set the identifier to "showItemSegue". I get an error in these two lines:
var detailController = segue.destinationViewController as ShowItemViewController
detailController.currentId = nextId!
Image illustration:
The code:
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
nextId = itemArray?.objectAtIndex(indexPath.row).objectForKey("objectId") as? NSString
self.performSegueWithIdentifier("showItemSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "showItemSegue") {
var detailController = segue.destinationViewController as ShowItemViewController
detailController.currentId = nextId!
}
}
The destination view controller of the segue is the UINavigationController. You need to ask it for its top view controller to get to the real destination view controller:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showItemSegue" {
let navController = segue.destination as! UINavigationController
let detailController = navController.topViewController as! ShowItemViewController
detailController.currentId = nextId!
}
}

Passing Data between View Controllers in Swift

I am trying to convert an app from Objective-C to Swift but I can't find how to pass data between views using Swift. My Objective-C code is
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
AnsViewController *ansViewController;
ansViewController = [storyBoard instantiateViewControllerWithIdentifier:#"ansView"];
ansViewController.num = theNum;
[self presentViewController:ansViewController animated:YES completion:nil];
What that is doing is it basically takes the variable, theNum, and passes it to the variable, num, on a different view controller. I know this may be an easy question but I am getting pretty confused with Swift so if someone could explain how they changed it to Swift that would be greatly appreciated!
Thanks
Let's assumed we stand at the firstView go to the DetailView and want passing data from firstView to Detailview. To do that with storyboard, at the firstView we will have a method:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "segueTest") {
//Checking identifier is crucial as there might be multiple
// segues attached to same view
var detailVC = segue!.destinationViewController as DetailViewController;
detailVC.toPass = textField.text
}
}
and then into the class of DetailView we declared a variable:
var toPass: String!
then you can use the variable toPass (of course you can change the type of the variable as you want, in this EX I just demo for string type).
class AnsViewController: UIViewController {
var theNum: Int
override func viewDidLoad() {
super.viewDidLoad()
println(theNum)
}
}
override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
let viewController = self.storyboard.instantiateViewControllerWithIdentifier("ansView") as AnsViewController
viewController.num = theNum
self.presentViewController(viewController, animated: true, completion: nil)
}
To pass string or any data from one controller to another in swift.
Follows below steps:
1) Create property in child controller as var abc:string!
2) Create object of childcontroller
let storyboard:UIStoryboard()
let viewController: childcontroller = storyboard.instantiateViewControllerWithIdentifier("childcontroller") as! childcontroller
viewController.abc = "hello";
self.navigationController.pushviewController(Controller:viewController animated:true CompletionHandler:nil)
Using tableview,
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let ClubProfileView = self.storyboard?.instantiateViewController(withIdentifier: "CBClubProfileViewController") as! CBClubProfileViewController
let TempCulubDic:NSDictionary =
((ClubsListbyDateDic.object(forKey:((ClubsListbyDateDic.allKeys as! [String]).sorted(by: <)as NSArray).object(at: indexPath.section) as! String) as! NSArray).object(at: indexPath.row))as! NSDictionary
let ClubId:String=(TempCulubDic.value(forKey: "club_id") as? String)!
let CheckIndate:String=(TempCulubDic.value(forKey: "chekin_date") as? String)!
ClubProfileView.ClubID=ClubId
ClubProfileView.CheckInDate = CheckIndate
// self.tabBarController?.tabBar.isHidden=true
ClubProfileView.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(ClubProfileView, animated: true)
}
#IBAction func nextbtnpreesd(_ sender: Any) {
let mystring = "2"
performSegue(withIdentifier: "MusicVC", sender: mystring)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? MusicVC{
if let song = sender as? String{
destination.strlablevalie = song
}
}
}
//in MusicVC create string like:
var strlablevalie:String!
To pass string or any data from one controller to another in swift.
Follows below steps:
1) Create variable of type which you want like (String,Int)
var test : String!
2) Create object of childcontroller
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Here identifier of your VC") as! (Here Name Of Your Controller)
vc.test = "Hello" (Or any data which you want to pass)
self.navigationController?.pushViewController(VC, animated: true)
That should solve your problem
Note: if we are using storyboard
Step 1: Master controller :
// table row which row was selected
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
println("You selected cell #\(indexPath.row)!")
nextScreenRow = indexPath.row
// get to the next screen
self.performSegueWithIdentifier("dashboard_static_screen_segue", sender: self)
}
and then;
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "dashboard_static_screen_segue") {
var detailController = segue.destinationViewController as StaticScreens;
detailController.screenNumber = nextScreenRow
}
}// end prepareForSegue
step 2: Detail controller (StaticScreen)
// set variable into your detail controller
var screenNumber: NSInteger?
println("selected row \(screenNumber!)")

Resources