使用此代码动态创建页面,这将创建一个PageViewController
import UIKit
class PageViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
var pvc:UIPageViewController?
var testArray: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
pvc = UIPageViewController(
transitionStyle: .Scroll,
navigationOrientation: .Horizontal,
options: nil )
// Make this object the datasource and delegate for the PageViewController
self.pvc?.delegate = self
self.pvc?.dataSource = self
// Populate an array of test content for the pages
testArray = ["Page 1", "Page 2", "Page 3"]
// Create an array of child pages
var swipePage:SwipePageController = SwipePageController()
println("Page view Controller: \(testArray[0])")
swipePage.name = testArray[0]
var pageArray: NSArray = [swipePage]
self.pvc?.setViewControllers(pageArray, direction: .Forward, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
let index:Int! = (viewController as SwipePageController).index!
if (index >= self.testArray.count){
return nil
}
index + 1
return viewControllerAtIndex(index)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
let index:Int! = (viewController as SwipePageController).index!
if index == NSNotFound {
return nil
}
index - 1
return viewControllerAtIndex(index)
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return self.testArray.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
let page = self.pvc?.viewControllers[0] as SwipePageController
let name = page.name
return find(self.testArray, name!)!
}
func viewControllerAtIndex(index: Int) -> SwipePageController?
{
if self.testArray.count == 0 || index >= self.testArray.count
{
return nil
}
// Create a new view controller and pass suitable data.
let swipeView = SwipePageController()
swipeView.name = self.testArray[index]
swipeView.index = index
return swipeView
}
println()大约是代码的1/3,显示了我期望看到的值。我动态创建的ViewController的代码在这里:
import UIKit
class SwipePageController: UIViewController {
// var to hold the var to display to the interface
var name:String?
var index:Int?
@IBOutlet weak var testLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
println(name!)
if let passedName = name? {
self.testLabel.text = passedName
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// 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.
}
*/
}
println(name!)也显示正确的值。但是,我无法使用值填充标签。我收到以下错误:
"致命错误:在展开可选值时意外发现nil"
我的智慧结束了...我无法弄清楚什么是不起作用的。另外,请参阅发生错误的Xcode界面的屏幕截图:
有人可以帮忙吗?
答案 0 :(得分:1)
我认为问题在于您实例化SwipePageController。
而不是:
var swipePage:SwipePageController = SwipePageController()
试试这个:
var swipePage: SwipePageController = UIStoryboard(name: "YourStoryboardName", bundle: nil).instantiateViewControllerWithIdentifier("YourViewControllerId")
将“YourStoryboardName”替换为故事板文件的实际名称,将“YourViewControllerId”替换为滑动页面控制器的Storyboard Id。如果您还没有为其提供Storyboard ID,请确保在Identity检查器中设置它。