从Swift中的另一个ViewController访问变量 [英] Accessing variables from another ViewController in Swift

查看:84
本文介绍了从Swift中的另一个ViewController访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 ViewController

class PageContentViewController: UIViewController {

    var pageIndex: Int

}

我如何从另一个ViewController访问 pageIndex

How would I access pageIndex from another ViewController?

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

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


推荐答案

swift中默认的一切都是public,因此如果你声明这样的东西:

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

这篇关于从Swift中的另一个ViewController访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆