2015-10-05 66 views
-2

我複製這個代碼的滑動視圖控制器從像菜單視頻的YouTube snapchat,但它不會編譯。我試圖創建一個幻燈片ViewController像snapchats

import UIKit 

class ViewController: UIViewController { 
    @IBOutlet weak var scrollView: UIScrollView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 


     var V1 : View1 = View1(nibName: "View1" , bundle: nil) 

     self.addChildViewController(V1) 
     self.scrollView.addSubview(V1.view) 
     V1.didMoveToParentViewController(self) 


    } 

我不斷收到錯誤使用未聲明的類型'視圖1'。我該如何解決?我跟着一個教程,代碼是相同的。我如何將View1聲明爲一種類型?

+0

因爲編譯器不知道'View1'是什麼。你可以分享該教程的鏈接嗎? –

回答

1

我做了同樣的教程,但對於三種視圖:

@IBOutlet weak var scrollView: UIScrollView! 

    override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    let view1: View1 = View1(nibName: "View1", bundle: nil) 

    addChildViewController(view1) 
    scrollView.addSubview(view1.view) 
    view1.didMoveToParentViewController(self) 

    let view2: View2 = View2(nibName: "View2", bundle: nil) 

    addChildViewController(view2) 
    scrollView.addSubview(view2.view) 
    view2.didMoveToParentViewController(self) 

    var view2Frame: CGRect = view2.view.frame 
    view2Frame.origin.x = view.frame.width 
    view2.view.frame = view2Frame 


    let view3: View3 = View3(nibName: "View3", bundle: nil) 

    addChildViewController(view3) 
    scrollView.addSubview(view3.view) 
    view3.didMoveToParentViewController(self) 

    var view3Frame: CGRect = view3.view.frame 
    view3Frame.origin.x = view.frame.width * 2 
    view3.view.frame = view3Frame 

    self.scrollView.contentSize.width = view.frame.width * 3 
    } 

這工作得很好。希望能幫助到你。