How do I access an array from one viewcontroller in another viewcontroller - ios

I am very new to the swift language and am trying to make an app that would do some basic calculations. The problem I am running into is that I want to use the values from an array that is declared in one viewcontroller in another viewcontroller to do calculations.
I declare the array in the first viewcontroller like this:
var array2 = [Double]()
And then I have no idea how to access this in the second view controller. I have tried looking at previous questions as well as online tutorials and have had no success.
Any help is greatly appreciated. Thank you in advance.

in VC2 create global var array2 = [Double]()
add following override func to body of VC1
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let DVC = segue.destinationViewController as! ViewController2 //replace ViewController2 with the name of your 2nd VC
DVC.array2 = array2
}
Overall, what you are doing is telling VC1 to copy VC1's array2 to VC2's array2, before the segue happens.
As your app becomes more complex, if your VC1 has more than 1 segue...meaning can go to multiple different VC's...you'll need to change your prepareForSegue so that it takes into account for this.
One way to do this is to change it to
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SegueIdentifier you give to that particular segue in identityInspector" {
let DVC = segue.destinationViewController as! ViewController2 //replace ViewController2 with the name of your 2nd VC
DVC.array2 = array2
}
}

Related

Swift & Navigation : How do I replace a ViewController and also send some parameters?

How do I replace a ViewController and also send some parameters?
With the code below, I can only push another ViewController into my current Navigation Stack.
Can we just pop and push another one immediately? Or is there an official to replace for good? Please kindly show me. Thank you.
Perform Segue
performSegue(withIdentifier: "ToNewViewController", sender: self)
Prepare Segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! NewViewController
destinationVC.params = params
}
If you have a navigation controller as your app window root , then you can manage all viewControllers
var vcs = self.navigationController!.viewControllers // get all vcs
vcs = vcs.dropLast() // remove last vc
let vc = // create new VC
vcs.append(vc) // append it
Then alter that array and set it back like this
self.navigationController!.setViewControllers(vcs,animated:true)
OR
You can get the top presented vc with this , then add a method to that vc to do the data replacement and refresh of your UI

prepareForSegue destination TableViewController

I currently have a button set to go to a TableViewController but decided I wanted to embed that TableViewController in a TabBarController. Well before I did this I had this bit of code in the previous ViewController.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "showListSegue"){
let des = segue.destinationViewController as! TableViewController
des.jsonfile = self.jsonfile
}
}
Now when I try and click the button it give me this error.
Could not cast value of type 'UITabBarController' (0x111c818b0) to
'Forkit.TableViewController' (0x10d1b1490).
I tried the following code with no success as it gives me this error
Value of type "UITabBarController" has no member 'jsonfile'.
let des = segue.destinationViewController as! TableViewController
How can I get around this?
The segue showListSegue now has your TabBarController as it's destinationViewController. This is why it can not cast it to TableViewController, because it's not the one you're looking for. The TabBarController does contain a reference to the ViewController you want though. You likely have two options:
1: Use .viewControllers and find your view in there, like this:
let tabBarController = segue.destinationViewController as! UITabBarController
let des = tabBarController.viewControllers![0]
In which [0] selects which tab's ViewController you need.
2: Select the right tab and use .selectedViewController
let tabBarController = segue.destinationViewController as! UITabBarController
tabBarController.selectedIndex = 0 // choose which tab is selected
let des = tabBarController.selectedViewController!
Please be aware that you are working with optional values here, so either be absolutely sure you can safely unwrap, or build in checks (casting with as? and if let statements, etc.).
More information: UITabBarController Class Reference
Try This
#IBAction func about(sender: AnyObject) {
performSegueWithIdentifier("about", sender: sender)
}

segue.destinationVC is nil in prepareForSegue

I've looked around many threads and haven't benefitted with my issue. My issue lies in my 3rd VC where the user taps an 'Edit UIBarButtonItem' which segues them to the 4th VC. The segue ID is correct and both VC's 3 and 4 are static table VC's, but 4 is embedded in a nav controller and 3 isn't. i want the data in the textfields in VC 3 to be 'copied' to the almost identical looking textfields in VC 4, so that changes can be made by the user and updated. When i type cast my destination segue as the 4th VC inside prepareForSegue, it returns nil and the instance of data inside doesn't get moved to the 4th VC since 'if let' is being used. Why does it return nil? I've looked up the life cycle of segues too but haven't learnt much, all other VC's are fine.
I have tried both below and both return nil for VC 4
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "EditSegue" {
print("correct segue name")
if let editVC: EditTableViewController = segue.destinationViewController as? EditTableViewController {
editVC.instanceOfData = self.instanceOfData //instance of data in 4th VC = instanceOfData in 3rd VC
}
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let editVC = segue.destinationViewController as? EditTableViewController
if segue.identifier == "EditSegue" {
editVC.instanceOfData = self.instanceOfData
}
}
I could be doing something obviously wrong which i haven't come across yet! I've successfully transferred data between two VC's before but not when instances have been passed around between VC's.
Any help is appreciated, thanks.

How to seque to ViewController inside Navigation Controller inside TabBarController

This is a totally dumb question I am sure. I have a viewcontroller that is inside a navigation controller that is inside a TabBarController. Apple says this is the right way to implement that setup. However, how can I prepareForSeque to that complex and send data to the first ViewController.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "presentActionItems" {
println("preparing")
let tabBarController:actionItemsTabController = segue.destinationViewController as! actionItemsTabController
let navigationController = ???
let viewController = ???
viewController.givenURL = actionItemsURL
}
}
I am sure this is an easy question, can I have some help.
Try this in your TabController's viewDidLoad after passing it your data
let firstViewController : UIViewController = self.viewControllers.objectAtIndex(0).topViewController;
firstViewController.this = self.this;
firstViewController.that = self.that;

Passing data behind Naviagtion Controller

Hi I have 4 view Controllers
MainVC ----> NavBar ----> MyViewController----->UIWebView
Inside my MainVC based on segue, I want to set different data to tableview controller. Here is my prepareforsegue method
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var navController = segue.destinationViewController as UINavigationController
var HVController = navigationController?.viewControllers[0] as MyViewController
HVController.somevar = "Something"
}
However program crashes. How can I pass data from my MainVC to MyViewController? Thanks.
EDIT: I have found my stupid mistake the code should have been as
var HVController = navController.viewControllers[0] as MyViewController
Sorry for wasting your time.
First you have to add a variable in your Target ViewController (MyViewController).
For example:
var passValue:String!
Then, you can pass value from your current ViewController to the target:
if segue.identifier == "yourSegueIdentifier" {
(segue.destinationViewController as MyViewController).passValue = yourObjectToPass
}

Resources