我正在尝试学习基本的iOS Swift3并使用故事板方法。我正在处理the tutorial on this website中的代码。
创建了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 :(得分: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语法
修改>转换>当前的Swift语法......
https://i.stack.imgur.com/niayG.png
答案 1 :(得分:0)
您需要使用
UIApplication.shared
和
在Swift 3中的UIScreen.main
。(暂不公开。)