在我的CathyTaskLogMessageViewController
课程中,我有以下代码:
import UIKit
class CathyTaskLogMessageViewController: UIViewController {
@IBOutlet var tapGestureRecognizer: UITapGestureRecognizer!
@IBOutlet weak var messageTextView: UITextView!
var defaultPictureButton: UIButton = UIButton(type: UIButtonType.Custom) as UIButton
override func viewDidLoad() {
super.viewDidLoad()
defaultPictureButton = UIButton(type: UIButtonType.Custom) as UIButton
defaultPictureButton.frame = CGRectMake(100, 100, 40, 40)
defaultPictureButton.addTarget(self, action: "defaultPictureButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
defaultPictureButton.setImage(UIImage(named: "defaultPicture"), forState: UIControlState.Normal)
defaultPictureButton.layer.cornerRadius = 20.0
defaultPictureButton.clipsToBounds = true
defaultPictureButton.exclusiveTouch = true
self.view.addSubview(defaultPictureButton)
}
func defaultPictureButtonTapped(sender: UIButton) {
let imageViewerViewController = ImageViewerViewController()
imageViewerViewController.image = self.defaultPictureButton.imageView!.image
imageViewerViewController.centerPictureFromPoint(self.defaultPictureButton.frame.origin, ofSize: self.defaultPictureButton.frame.size, withCornerRadius: self.defaultPictureButton.layer.cornerRadius)
}
}
在我的ImageViewerViewController
课程中,我有以下代码:
import UIKit
class ImageViewerViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var image: UIImage!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func centerPictureFromPoint(point: CGPoint, ofSize size: CGSize, withCornerRadius radius: CGFloat) {
self.imageView = UIImageView(frame: CGRect(x: point.x, y: point.y, width: size.width, height: size.height))
self.imageView.layer.cornerRadius = radius //I am getting the error here
self.imageView.clipsToBounds = true
self.imageView.image = self.image
}
}
我得到的错误是:fatal error: unexpectedly found nil while unwrapping an Optional value
。在我看来,我通过编写imageView
来初始化self.imageView = UIImageView(frame: CGRect(x: point.x, y: point.y, width: size.width, height: size.height))
。但是,它返回零。有人有解决方案吗?
答案 0 :(得分:3)
如果您不想通过xib或storyboard启动imageView。从:
更改属性声明@IBOutlet weak var imageView: UIImageView!
到
var imageView: UIImageView!
弱会导致imageView立即释放。