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

时间:2014-06-06 01:23:22

标签: ios objective-c swift uipageviewcontroller

我有以下ViewController

class PageContentViewController: UIViewController {

    var pageIndex: Int

}

我如何从另一个ViewController访问pageIndex

我需要在此功能中访问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 个答案:

答案 0 :(得分:21)

默认情况下,swift中的所有内容都是公开的,因此如果你声明这样的内容:

class SomeViewController: UIViewController {
    var someVariable: SomeType = someValue

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

只要你有一个实例,你就可以访问它:

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

答案 1 :(得分:8)

ViewController1.swift

class ViewController1: UIViewController {

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

ViewController2.swift

class ViewController2: UIViewController 
{

print(ViewController1.GlobalVariable.myString)

}

答案 2 :(得分:5)

我解决了这个问题。 。

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

class PageContentViewController: UIViewController
{

    var pageIndex: Int
    StaticLinker.viewController = self
}

class AnotherClass
{
    StaticLinker.viewController.pageIndex = 100
}

答案 3 :(得分:2)

这是我尝试将该代码转换为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 :(得分:1)

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

您可以通过创建firstViewController()

的实例来访问secondViewController中的此变量
class secondViewController: UIViewController{
var newVariableName = firstViewController()
}

稍后使用newVariableName访问variableName的值

newVariableName.variableName

答案 5 :(得分:1)

当我必须在每个其他类中使用令牌时,我面临着相同的问题,解决方案非常简单,我所做的就是

var access = LoginViewController() 
token = access.token

,但是遇到的问题是:使用令牌的默认值。 所以我将该变量设为静态,并在视图中使用了此变量。

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

答案 6 :(得分:0)

class color: UIViewController {

    var blue:Int = 90
}

从其他类访问它(确保在函数内调用变量,否则你会得到错误)

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


  override func viewDidLoad() {
     super.viewDidLoad()
     // call it here
      print(example.blue)
  }
}