-1

I am trying to create an app where you can swipe between veiws (Similar to snapchat) and I came across this example here

Now I tried this out and it works exactly how I wanted it to. When I try implementing the same on my app I run into some issues.

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.
    }
    */

}

Now I marked in the code where the issue is occurring. I keep getting the following issue:

fatal error: unexpectedly found nil while unwrapping an Optional value

And I am not sure why. I have tried debugging, but I am relatively new to swift/ios development so I may be overlooking something.

mufc
  • 695
  • 3
  • 16
  • 31
  • you can set a breakpoint in xcode and see which line is giving error. – ArunGJ May 18 '16 at 20:47
  • You didn't hook the outlet up to an object in interface builder. It's nil because you didn't initialize it programmatically or from a nib in IB – Dare May 18 '16 at 20:57

1 Answers1

1

I think

@IBOutlet var scrollView: UIScrollView?;

should be:

@IBOutlet var scrollView: UIScrollView!

Is @IBOutlet var scrollView: UIScrollView? mapped with your viewController?

ZGski
  • 2,398
  • 1
  • 21
  • 34
Arnold Mapps
  • 190
  • 3
  • 11