![](/img/trans.png)
[英]unexpectedly found nil while unwrapping an Optional value On UILabel
[英]UILabel - unexpectedly found nil while unwrapping an Optional value
使用此代码动态创建页面,这将创建一个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
}
在代码的大约1/3处,println()确实显示了我期望看到的值。 我动态创建的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界面的屏幕截图:
有人可以帮忙吗?
我认为问题在于您实例化SwipePageController的方式。
代替这个:
var swipePage:SwipePageController = SwipePageController()
尝试这个:
var swipePage: SwipePageController = UIStoryboard(name: "YourStoryboardName", bundle: nil).instantiateViewControllerWithIdentifier("YourViewControllerId")
将“ YourStoryboardName”替换为情节提要文件的实际名称,将“ YourViewControllerId”替换为“滑动页面控制器”的情节提要ID。 如果尚未为其指定情节提要ID,请确保在身份检查器中进行设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.