Accessing Variables from Another Viewcontroller in Swift

Accessing variables from another ViewController in Swift

Everything by default in swift is public, and thus if you declare something like this:

class SomeViewController: UIViewController {
var someVariable: SomeType = someValue

init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
}

You can access it as long as you have an instance of it:

var myCustomViewController: SomeViewController = SomeViewController(nibName: nil, bundle: nil)
var getThatValue = myCustomViewController.someVariable

Using variables from one View Controller in another

I'd suggest a 3rd class to abstract Player info and make it Singleton. Something like this:

class Players {
static let shared = Players()

var one: String {
get {
return UserDefaults.standard.object(forKey: "playerone") as? String ?? ""
}
set(newValue) {
UserDefaults.standard.set(newValue, forKey: "playerone")
}
}
}

And access/set same like this:

Players.shared.one = "Name one"
print(Players.shared.one)

Accessing ViewController's variables through another ViewController

To pass arguments between View Controllers, you can use segues.

First you have the variable in FirstViewController

FirstViewController: UIViewController {
var name: String = "test"
...
}

Then you have a variable of the same type in SecondViewController

SecondViewController: UIViewController {
var name: String = ""
...
}

To move from FirstViewController, you use a programmatic segue.

self.performSegue(withIdentifier: "Indentifier", sender: nil)

Then, in FirstViewController, define prepareForSegue:sender.
You get a reference to the destination view controller, then set the variable to the one from sending view controller

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! SecondViewController
vc.name = name
}

EDIT:
If you need it to be accessible in all view controllers, define a Global class. You stated in your question you tried a struct. Instead, try static variables

class Global {   
static var name: String?
}

EDIT 2:
Another way to have global variables is with a singleton class

class Global {
static let sharedInstance = Global()
var name: String?
}

Access a variable from another view controller. Swift

This code makes no sense:

    if topViewController() is ConversationViewController {
let myCustomViewController: ConversationViewController = ConversationViewController(nibName: nil, bundle: nil)

Translation:

"If the top view controller is a ConversationViewController, create a new, empty instance of ConversationViewController, and ask that instance for it's otherProfileName value"

It's like looking through a set of boxes for the blue box, since you know it contains an apple. When you find a blue box, go build a new, empty blue box, open it, and wonder why it doesn't contain your apple.

Access to a variable from another view controller

If you want bool variable to be accessible from other viewController.Then simply wirte it as :-

@property BOOL Contact;

and make an object of ViewController in which you have declared contact variable as BOOL and access this variable using like this:-

OtherViewController *otherViewController=[[OtherViewController alloc] init];
otherViewController.Contact=YES;

As it is a instance variable it has to be accessed using class object.

Accessing ViewController's variables through another ViewController

To pass arguments between View Controllers, you can use segues.

First you have the variable in FirstViewController

FirstViewController: UIViewController {
var name: String = "test"
...
}

Then you have a variable of the same type in SecondViewController

SecondViewController: UIViewController {
var name: String = ""
...
}

To move from FirstViewController, you use a programmatic segue.

self.performSegue(withIdentifier: "Indentifier", sender: nil)

Then, in FirstViewController, define prepareForSegue:sender.
You get a reference to the destination view controller, then set the variable to the one from sending view controller

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! SecondViewController
vc.name = name
}

EDIT:
If you need it to be accessible in all view controllers, define a Global class. You stated in your question you tried a struct. Instead, try static variables

class Global {   
static var name: String?
}

EDIT 2:
Another way to have global variables is with a singleton class

class Global {
static let sharedInstance = Global()
var name: String?
}

How to access variable from one class in another? Swift Code

Create a following variable in ViewController2

var previousViewController: ViewController!

Add following line of code in your ViewController Class

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){

if segue.destinationViewController .isKindOfClass(ViewController2){
let vc2 = segue.destinationViewController as! ViewController2
vc2.previousViewController = self
}
}

Now in viewDidLoad method of ViewController2 you can access bagCenter like below:

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

var bagCenter = previousViewController.transferViewControllerVariables()
}


Related Topics



Leave a reply



Submit