在Swift中访问另一个ViewController中的变量

时间:2022-06-06 16:02:55

I have the following ViewController:

我有下面的ViewController:

class PageContentViewController: UIViewController {

    var pageIndex: Int

}

How would I access pageIndex from another ViewController?

如何从另一个视图控制器访问pageIndex ?

I need to access pageIndex in this function:

我需要在这个函数中访问pageIndex:

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?
}

7 个解决方案

#1


21  

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

默认情况下,swift是公开的,因此,如果您声明如下:

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

#2


5  

ViewController1.swift

ViewController1.swift

class ViewController1: UIViewController {

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

ViewController2.swift

ViewController2.swift

class ViewController2: UIViewController 
{

print(ViewController1.GlobalVariable.myString)

}

#3


3  

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

以下是我将代码转换为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?

}

#4


3  

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
}

#5


1  

class firstViewController: UIViewController{
var variableName: variableType = variableValue
}

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

您可以通过创建firstViewController()的实例来访问secondViewController中的这个变量

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

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

稍后使用newVariableName来访问该variableName的值

newVariableName.variableName

#6


1  

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

我遇到了同样的问题当我必须在其他类中使用token时,解决方案非常简单,我所做的就是

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.

但是它遇到的问题是:使用默认值作为令牌。所以我把这个变量设为静态,并使用了view中的load。

     var token : String = ""
     var access = LoginViewController.self
     token = access.token

#7


0  

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


21  

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

默认情况下,swift是公开的,因此,如果您声明如下:

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

#2


5  

ViewController1.swift

ViewController1.swift

class ViewController1: UIViewController {

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

ViewController2.swift

ViewController2.swift

class ViewController2: UIViewController 
{

print(ViewController1.GlobalVariable.myString)

}

#3


3  

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

以下是我将代码转换为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?

}

#4


3  

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
}

#5


1  

class firstViewController: UIViewController{
var variableName: variableType = variableValue
}

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

您可以通过创建firstViewController()的实例来访问secondViewController中的这个变量

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

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

稍后使用newVariableName来访问该variableName的值

newVariableName.variableName

#6


1  

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

我遇到了同样的问题当我必须在其他类中使用token时,解决方案非常简单,我所做的就是

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.

但是它遇到的问题是:使用默认值作为令牌。所以我把这个变量设为静态,并使用了view中的load。

     var token : String = ""
     var access = LoginViewController.self
     token = access.token

#7


0  

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