我有以下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? }
解决方法
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