ios 在 Swift 中从另一个 ViewController 访问变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24072766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:34:31  来源:igfitidea点击:

Accessing variables from another ViewController in Swift

iosobjective-cswiftuipageviewcontroller

提问by fulvio

I have the following ViewController:

我有以下几点ViewController

class PageContentViewController: UIViewController {

    var pageIndex: Int

}

How would I access pageIndexfrom another ViewController?

我如何pageIndex从另一个 ViewController访问?

I need to access pageIndexin 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?
}

采纳答案by Mike

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

回答by Sabarinathan

ViewController1.swift

ViewController1.swift

class ViewController1: UIViewController {

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

ViewController2.swift

ViewController2.swift

class ViewController2: UIViewController 
{

print(ViewController1.GlobalVariable.myString)

}

回答by roy

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
}

回答by Connor

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?

}

回答by Lakshay

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.

但它遇到的问题是:使用token的默认值。所以我将该变量设为静态并在视图中使用它确实加载了。

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

回答by Lakshay

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

回答by Rawand Saeed

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