25

I have the following ViewController:

class PageContentViewController: UIViewController {

    var pageIndex: Int

}

How would I access pageIndex from another ViewController?

I need to access pageIndex in this function:

func pageViewController(pageViewController: UIPageViewController!, viewControllerBeforeViewController viewController: UIViewController!) -> UIViewController! {

    //var index: Int
    //index = ?

    NSUInteger index = ((PageContentViewController*) viewController).pageIndex; // in Swift?

    if ((index == 0) || (index == NSNotFound)) {
        return nil;
    }

    index--;

    return [self viewControllerAtIndex:index]; // in Swift?
}
Shruti Thombre
  • 989
  • 4
  • 11
  • 27
gotnull
  • 26,454
  • 22
  • 137
  • 203
  • 2
    the answer of this question is same as other "Accessing variables from another class" questions. you need to get reference of the object first. (and I think it is a bad question) – Bryan Chen Jun 06 '14 at 01:52
  • `var index: Int = PageContentViewController(viewController).pageIndex` that's how you could translate that line into Swift I believe – Connor Pearson Jun 06 '14 at 01:54
  • 2
    @BryanChen: You think or you know? A link would be great to an existing answer. I'd be more than happy to close this once you've provided that. – gotnull Jun 06 '14 at 01:55

7 Answers7

24

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
gotnull
  • 26,454
  • 22
  • 137
  • 203
Mike
  • 9,765
  • 5
  • 34
  • 59
  • 1
    does this only work if the classes are in the same file? – Connor Pearson Jun 06 '14 at 01:48
  • 2
    this is not a valid reason to use global variable. and what happen if you have multiple instance of `PageContentViewController`? – Bryan Chen Jun 06 '14 at 01:48
  • 3
    @BryanChen: By all means, show us how to properly access the variable then. – gotnull Jun 06 '14 at 01:49
  • @gotnull partially. I'd imagine they'd have to be in the same file since it's declared at the file scope but I'm not sure – Connor Pearson Jun 06 '14 at 01:49
  • 1
    No it doesn't need to be in the same file, but I'll edit this with a better answer. – Mike Jun 06 '14 at 01:51
  • __EVERYTHING__ is swift is public (at moment) – Bryan Chen Jun 06 '14 at 01:55
  • @BryanChen: I'm yet to see an attempted answer at my question. – gotnull Jun 06 '14 at 01:57
  • 1
    @gotnull Have you tried my edited solution? pretty straightforward I think. – Mike Jun 06 '14 at 01:58
  • Typo, and my code is an example with random test code I wrote. Add a function to your PageContentViewController that returns the variable you need to access and then call that function on an instance of the PageContentViewController you had. – Mike Jun 06 '14 at 02:00
  • That's because you dont have TestViewController.... I created that as an example. You never created that class. See my edit. – Mike Jun 06 '14 at 02:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55169/discussion-between-mike-and-gotnull). – Mike Jun 06 '14 at 02:09
  • using swift 3, would i need to override? it wants me to add a fatalError, and fails when ran. – Dylan Feb 24 '17 at 15:35
  • can you give a real example please ? @Mike – Dory Daniel Mar 13 '17 at 11:02
13

ViewController1.swift

class ViewController1: UIViewController {

    struct GlobalVariable{
        static var myString = String()
    }
}

ViewController2.swift

class ViewController2: UIViewController 
{

print(ViewController1.GlobalVariable.myString)

}
Pang
  • 9,564
  • 146
  • 81
  • 122
  • Annoying that you then have to call GlobalVariable.myString even in ViewController1 and can't just refer to myString there. But this works well for referencing it in the other view controller. Thanks! – RanLearns May 17 '17 at 23:53
  • you don't need to add the extra struct of GlobalVariable. you can simply make myString either static var myString or class var myString directly in the Viewcontroller1 class. Then to use it simply use it like print(Viewcontroller1.myString) – Mark Dail Aug 16 '18 at 17:03
5

I solved this way . .

class StaticLinker
{
     static var viewController : PageContentViewController? = nil
}

class PageContentViewController: UIViewController
{

    var pageIndex: Int
    StaticLinker.viewController = self
}

class AnotherClass
{
    StaticLinker.viewController.pageIndex = 100
}
roy
  • 6,685
  • 3
  • 26
  • 39
2

Here's my attempt to convert that code to swift:

func pageViewController(pageViewController: UIPageViewController!, viewControllerBeforeViewController viewController: UIViewController!) -> UIViewController! {

    var pageController  = viewController as PageContentViewController
    var index: Int? = pageController.pageIndex

    if var idx: Int = index{
        if idx == 0{
            return nil
        }
        else{
            idx--
            return viewControllerAtIndex(idx)
        }
    }
    else{
        return nil
    }
}
class PageContentViewController: UIViewController {

    var pageIndex: Int?

}
Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
2

I was facing the same problem when i have to use token in every other class, the solution is pretty simple, What i did was

var access = LoginViewController() 
token = access.token

but the problem it encountered was: Using the default value for token. so i made that variable static and used this in view did load.

     var token : String = ""
     var access = LoginViewController.self
     token = access.token
Lakshay
  • 180
  • 1
  • 10
1
class color: UIViewController {

    var blue:Int = 90
}

access it from other class (make sure to call the variable within a function otherwise you gonna get error )

class ViewController: UIViewController{
   var example:color = color()


  override func viewDidLoad() {
     super.viewDidLoad()
     // call it here
      print(example.blue)
  }
}
1
class firstViewController: UIViewController{
var variableName: variableType = variableValue
}

You can access this variable in the secondViewController by creating an instance of firstViewController()

class secondViewController: UIViewController{
var newVariableName = firstViewController()
}

later on use the newVariableName to access the value of the variableName

newVariableName.variableName
Rawand Saeed
  • 795
  • 10
  • 13