Global variable returning nil value when used in diffrent view controller

Krishna Jayachandran

I have 4 global variables (String arrays) which i am appending in one view controller(say firstViewController). After appending values when i try to access them in another view controller a nil value is being passed

First ViewController

print("\(json)")
            if let stockInfo = json?[0] {

                if let value = stockInfo["l"] as? String {
                    print("Teja\(value)")
                    stockCurrentValue.append(value)
                    print("Teja\(stockCurrentValue)")

                }

                if let cp = stockInfo["cp"] as? String{
                    print("Teja\(cp)")
                    stockCp.append(cp)
                    print("Tea\(stockCp)")
                }

                if  let c = stockInfo["c"] as? String{
                    print("Teja\(c)")
                    stockC.append(c)

                }
                if let pcls_fix = stockInfo["pcls_fix"] as? String{

                    print("Teja\(pcls_fix)")
                    stockPrevValue.append(pcls_fix)

                }


            }

The variables stockCurrentValue,stockC, stockCP, stockPrevValue are the four global variables i declared.

But when i try to access them in a new viewController

SecondViewController

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    print(selectedStockNames.count)
    return selectedStockNames.count


}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "myStocks") as! StockCell

        cell.stockNameCell.text = selectedStockNames[indexPath.row]


    print("Teja123\(stockCurrentValue)")

    stockValueFromGF = selectedStockValues[indexPath.row]

    print("Teja123\(stockCp)")

    return cell
}

So when i print StockCp value in firstViewContoller i get the value but when i try to print the same variable in seconViewController i get "[]" (nil value)

Sriteja Chilakamarri

Use protocol and define it in the View Controller from where you want to set the values.

Use the method inside the function where the values of the global variables change.

Call the function and set the delegate in the view controller where you want to access the variables.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

popoverPresentationController is nil when used with a Navigation View Controller

From Dev

popoverPresentationController is nil when used with a Navigation View Controller

From Dev

Instance variable in view turns nil when used

From Dev

Variable nil when trying to pass variable to view controller with segue

From Dev

Variable nil when trying to pass variable to view controller with segue

From Dev

Assign a variable by returning a value from a block when assignment is nil

From Dev

Assign a variable by returning a value from a block when assignment is nil

From Dev

iOS share comman view on diffrent view controller

From Dev

Send variable value to next view controller when click a table cell

From Dev

extjs global variable always returning original value

From Dev

$_GET variable not returning value when used in a search function in MVC designed code

From Dev

Spring controller not returning value when not using ResponseEntity

From Dev

$rootscope value used in a controller is overridden when refreshing the page used with that controller

From Dev

attempt to index global variable OE (a nil value) Lua with sql

From Dev

Calling Global Variable from Controller to HTML View AngularJS

From Dev

Expected declaration while accessing global variable from second view controller

From Dev

Not navigating to other view controller, When passing bundle is nil in iOS?

From Dev

Returning to the root view controller

From Dev

Ruby Array#bsearch: operation and returning nil when value is not in Array

From Dev

Undefined variable in view when testing Laravel controller

From Dev

Variable not updating its value when Returning it

From Dev

Amazon Ad View returning nil

From Dev

Async Controller not returning anything to the view when querying multiple datatable in parallel

From Dev

Perform action when returning back to view controller via 'show' segue

From Dev

Tab bar disappears when returning from modal view controller

From Dev

Async Controller not returning anything to the view when querying multiple datatable in parallel

From Dev

Global variable in laravel controller

From Dev

global variable in application controller?

From Dev

How to change the value to a global variable in the scope using a function? ("controller as")

Related Related

  1. 1

    popoverPresentationController is nil when used with a Navigation View Controller

  2. 2

    popoverPresentationController is nil when used with a Navigation View Controller

  3. 3

    Instance variable in view turns nil when used

  4. 4

    Variable nil when trying to pass variable to view controller with segue

  5. 5

    Variable nil when trying to pass variable to view controller with segue

  6. 6

    Assign a variable by returning a value from a block when assignment is nil

  7. 7

    Assign a variable by returning a value from a block when assignment is nil

  8. 8

    iOS share comman view on diffrent view controller

  9. 9

    Send variable value to next view controller when click a table cell

  10. 10

    extjs global variable always returning original value

  11. 11

    $_GET variable not returning value when used in a search function in MVC designed code

  12. 12

    Spring controller not returning value when not using ResponseEntity

  13. 13

    $rootscope value used in a controller is overridden when refreshing the page used with that controller

  14. 14

    attempt to index global variable OE (a nil value) Lua with sql

  15. 15

    Calling Global Variable from Controller to HTML View AngularJS

  16. 16

    Expected declaration while accessing global variable from second view controller

  17. 17

    Not navigating to other view controller, When passing bundle is nil in iOS?

  18. 18

    Returning to the root view controller

  19. 19

    Ruby Array#bsearch: operation and returning nil when value is not in Array

  20. 20

    Undefined variable in view when testing Laravel controller

  21. 21

    Variable not updating its value when Returning it

  22. 22

    Amazon Ad View returning nil

  23. 23

    Async Controller not returning anything to the view when querying multiple datatable in parallel

  24. 24

    Perform action when returning back to view controller via 'show' segue

  25. 25

    Tab bar disappears when returning from modal view controller

  26. 26

    Async Controller not returning anything to the view when querying multiple datatable in parallel

  27. 27

    Global variable in laravel controller

  28. 28

    global variable in application controller?

  29. 29

    How to change the value to a global variable in the scope using a function? ("controller as")

HotTag

Archive