2017-01-24 123 views
-1

我想學習基本的iOS Swift3並用故事板的方法。我正在研究the tutorial on this website中的代碼。Segue公司沒有成員的ViewController

已經創造了FirstCustomSegue故事板的鏈接,我的代碼如下:我已爲所有6個錯誤添加了評論:

class FirstCustomSegue: UIStoryboardSegue { 

override func perform() { 
    // Value of type 'FirstCustomSegue' has no member 'sourceGameViewController' 
    var firstVCView = self.sourceGameViewController.view as UIView! 
    var secondVCView = self.destinationGameViewController.view as UIView! 

    // Cannot call value of non-function type 'UIScreen' 
    let screenWidth = UIScreen.mainScreen().bounds.size.width 
    let screenHeight = UIScreen.mainScreen().bounds.size.height 

    secondVCView.frame = CGRectMake(0.0, screenHeight, screenWidth, screenHeight) 

    // Cannot call value of non-function type 'UIApplication' 
    let window = UIApplication.sharedApplication().keyWindow 
    window?.insertSubview(secondVCView, aboveSubview: firstVCView) 

    // Value of type 'FirstCustomSegue' has no member 'sourceGameViewController' (This is in the (Finished) line, of course) 

    UIView.animateWithDuration(0.4, animations: {() -> Void in 
     firstVCView.frame = CGRectOffset(firstVCView.frame, 0.0, -screenHeight) 
     secondVCView.frame = CGRectOffset(secondVCView.frame, 0.0, -screenHeight) 

    }) { (Finished) -> Void inself.sourceGameViewController.presentGameViewController(self.destinationGameViewController as UIViewController, 
       animated: false, 
       completion: nil) 
    } 
} 

} 

在我看來好像某處存在缺失的連接,但我找不到它。也許有一些是iOS10的變化?一些搜索暗示這可能是UIScreen.mainScreen()位的問題,但我找不到解決問題的任何東西。

+0

你下面的教程是iOS 8的這是在斯威夫特2.x的編碼另一方面,你在Swift 3編碼。每當你看到這個**不能調用非函數類型的值'UIScreen **這意味着你正在調用的**不是一個函數,所以你應該刪除'()'。如果你輸入'UIScreen.',然後開始輸入m ...你會看到它只有主要的屬性,而不是一個函數。同樣的問題,你的'UIApplicaton'它應該是'UIApplication.shared' ...的經驗法則是,當您使用舊的教程,不要複製/粘貼,而不是鍵入,看看sytanx被更新。 – Honey

+0

此外**數值類型的「FirstCustomSegue」的錯誤沒有成員「sourceGameViewController **意味着你FirstCustomSegue **不**有名爲'ViewController'任何* *財產(也稱爲成員)。也許你命名你的財產別的東西和它的一個錯字或別該財產在所有 – Honey

+0

謝謝你 - 這是非常有幫助的。現在我知道如何推進這些事情!多謝。 – CAgrippa

回答

1

我重寫了你的代碼到Swift 3.0風格。但是你使用Xcode 8嗎?如果您使用Xcode 8,編譯器可能會幫助您解決語法錯誤。

class FirstCustomSegue: UIStoryboardSegue { 

    override func perform() { 
     let firstVCView: UIView = self.source.view 
     let secondVCView: UIView = self.destination.view 

     let screenWidth = UIScreen.main.bounds.size.width 
     let screenHeight = UIScreen.main.bounds.size.height 
     secondVCView.frame = CGRect(x: 0.0, y: screenHeight, width: screenWidth, height: screenHeight) 

     let window = UIApplication.shared.keyWindow 
     window?.insertSubview(secondVCView, aboveSubview: firstVCView) 

     UIView.animate(withDuration: 0.4, animations: { 
      firstVCView.frame = firstVCView.frame.offsetBy(dx: 0.0, dy: -screenHeight) 
      secondVCView.frame = secondVCView.frame.offsetBy(dx: 0.0, dy: -screenHeight) 
     }, completion: { [unowned self] finished in 
      self.source.present(self.destination, animated: false, completion: nil) 
     }) 
    } 
} 

你也可以自己將你的代碼轉換成最新的Swift語法。
編輯>轉換>當前斯威夫特語法...
https://i.stack.imgur.com/niayG.png

0

您需要使用

UIApplication.shared

UIScreen.main

斯威夫特3.(還不能發表評論。 )

+0

這會產生相同的錯誤消息:「無法調用非函數類型X的值」 – CAgrippa

+0

我複製粘貼您的類並將「UIScreen.mainScreen()。bounds.size.width」重命名爲「UIScreen.main.bounds.size」。寬度「,它絕對編譯。 – hoshy