如何以编程方式在UIPageViewControllers的子节点之间进行数据传输和传递。这必须这样做,因为我使用的项目如:TableViewCell点击和工具栏项目按钮。
我的UIPageViewController设置如下:
//
// RootViewController.swift
// slide
//
// Created by Justin Zollars on 2/12/15.
// Copyright (c) 2015 rmb. All rights reserved.
//
import UIKit
let didFinishUploadPresentNewPage = "com.snapAPI.presentNewPage"
class RootViewController: UIPageViewController, UIPageViewControllerDataSource {
var navViewController : UINavigationController!
var oneViewController : oneViewController!
var districtsViewController: DistrictsTableViewController!
var currentViewController:UIViewController? = nil
override func viewDidLoad() {
super.viewDidLoad()
NSLog("*****************************")
NSNotificationCenter.defaultCenter().addObserver(self, selector: "navigateHome", name: didFinishUploadPresentNewPage, object: nil)
// set UIPageViewControllerDataSource
self.dataSource = self
// Reference all of the view controllers on the storyboard
self.navViewController = self.storyboard?.instantiateViewControllerWithIdentifier("navViewController") as? CustomNavigation
self.navViewController.title = "One"
// println("homeTableViewController has landed!")
self.oneViewController = self.storyboard?.instantiateViewControllerWithIdentifier("oneViewController") as? oneViewController
self.oneViewController.title = "Three"
self.districtsViewController = self.storyboard?.instantiateViewControllerWithIdentifier("districtsViewController") as? DistrictsTableViewController
self.districtsViewController.title = "Two"
// println("Camera has landed!")
self.currentViewController = self.navViewController
// Set starting view controllers
var startingViewControllers : NSArray = [self.oneViewController]
self.setViewControllers(startingViewControllers, direction: .Forward, animated: false, completion: nil)
// println("Hey swab. C'mere. Listen up.")
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
switch viewController.title! {
case "Three":
return navViewController
case "One":
return districtsViewController
case "Two":
return nil
default:
return nil
}
}
// order: Two(districtsViewController) |One (navViewController) |camera (oneViewController)
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
switch viewController.title! {
case "One":
return oneViewController
case "Three":
return nil
case "Two":
return navViewController
default:
return nil
}
}
func navigateHome() {
var navigateToHome : NSArray = [self.navViewController]
self.setViewControllers(navigateToHome, direction: .Forward, animated: true, completion: nil)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
我甚至有一个糟糕的解决方案,我正在寻找正确的方法来做到这一点。
我使用Notifications和Singletons的组合,感觉就像一个丑陋的黑客。
import Foundation
class DataToAPI {
var hood: NSString!
class var sharedInstance :DataToAPI {
struct Singleton {
static let instance = DataToAPI()
}
return Singleton.instance
}
}
我在其他控制器中使用单例来共享这样的数据:
var sharedInstance = DataToAPI.sharedInstance
如果你正在阅读上面的代码,我会发出一个通知,呼叫navigateHome()
使用UIPageViewControllers查找(或使用其等效项)的正确方法是什么?