我无法获得以下代码进行编译,有人建议吗?
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var avatar: UIImageView!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var contentImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
avatar.layer.cornerRadius = 5.0
avatar.layer.borderWidth = 4.0
avatar.layer.borderColor = UIColor.whiteColor().CGColor
avatar.clipsToBounds = true
scrollView.delegate = self
contentImageView.clipsToBounds = true
}
override func viewDidAppear(animated: Bool) {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("LoginScreen") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
func scrollViewDidScroll(scrollView: UIScrollView) {
let yOffset = self.scrollView.contentOffset.y * 0.2
let availableOffset = min(yOffset, 60)
let contentRectYOffset = availableOffset / contentImageView.frame.size.height
contentImageView.layer.contentsRect = CGRectMake(0.0, contentRectYOffset, 1, 1);
}
}
错误
'来自'UIViewController的'Downcast?' 'UIViewController'只能解开 自选项目;你的意思是使用'!'
这发生在
行中let vc = self.storyboard?.instantiateViewControllerWithIdentifier("LoginScreen") as! UIViewController
答案 0 :(得分:0)
你强行抛弃一个已经是同一类型的对象。您只需删除as! UIViewController
并假设标识为ViewController
的{{1}}存在,您可以在需要时强制解包LoginScreen
变量。
vc
答案 1 :(得分:0)
感谢R P,您的解决方案很有效。
override func viewDidAppear(animated: Bool) {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("LoginScreen")
self.presentViewController(vc!, animated: true, completion: nil)
}