我正在尝试创建一个可以在浏览器之间滑动的应用程序(类似于Snapchat),我遇到了这个示例here
现在我尝试了这个,它完全符合我的要求。当我尝试在我的应用程序上实现相同时,我遇到了一些问题。
import UIKit
class ContainerViewController: UIViewController {
// MARK: Properties
@IBOutlet var scrollView: UIScrollView?;
override func viewDidLoad() {
super.viewDidLoad()
let goal1 :GoalOneViewController = GoalOneViewController(nibName: "GoalOneViewController", bundle: nil);
let goal2 :GoalTwoViewController = GoalTwoViewController(nibName: "GoalTwoViewController", bundle: nil);
let goal3 :GoalThreeViewController = GoalThreeViewController(nibName: "GoalThreeViewController", bundle: nil);
// 2) Add in each view to the container view hierarchy
// Add them in opposite order since the view hieracrhy is a stack
self.addChildViewController(goal3);
// ERROR OCCURS ON LINE BELOW ----------------------------
self.scrollView!.addSubview(goal3.view);
goal3.didMoveToParentViewController(self);
self.addChildViewController(goal2);
self.scrollView!.addSubview(goal2.view);
goal2.didMoveToParentViewController(self);
self.addChildViewController(goal1);
self.scrollView!.addSubview(goal1.view);
goal1.didMoveToParentViewController(self);
// 3) Set up the frames of the view controllers to align
// with eachother inside the container view
var adminFrame :CGRect = goal1.view.frame;
adminFrame.origin.x = adminFrame.width;
goal2.view.frame = adminFrame;
var BFrame :CGRect = goal2.view.frame;
BFrame.origin.x = 2*BFrame.width;
goal3.view.frame = BFrame;
// 4) Finally set the size of the scroll view that contains the frames
let scrollWidth: CGFloat = 3 * self.view.frame.width
let scrollHeight: CGFloat = self.view.frame.size.height
self.scrollView!.contentSize = CGSizeMake(scrollWidth, scrollHeight);
}
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.
}
*/
}
现在我在代码中标出了问题所在。我一直收到以下问题:
fatal error: unexpectedly found nil while unwrapping an Optional value
我不确定为什么。我已经尝试过调试,但我对swift / ios开发相对较新,所以我可能会忽略一些东西。
答案 0 :(得分:1)
我认为
@IBOutlet var scrollView: UIScrollView?;
应该是:
@IBOutlet var scrollView: UIScrollView!
@IBOutlet var scrollView: UIScrollView?
是否与您的viewController映射?