无法全局创建View。我的代码是:
required init(coder aDecoder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
}
// CODING
class ViewController: UIViewController {
//Unable to create View globally, kindly help me,,
//Error coming like this
required init(coder aDecoder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
}
var view11:UIView
var img_view11:UIImageView
.
.
.
@IBAction func long(sender: UILongPressGestureRecognizer)
{
var data1: NSData = UIImagePNGRepresentation(img.image)
view11 = UIView(frame: CGRect(x: 25, y: 0, width: 300, height: 200))
img_view11 = UIImageView(frame: CGRect(x: 0, y: 0, width: 300, height: 200))
img_view11.image = UIImage(data: data1)
view11.addSubview(img_view11)
self.vw.addSubview(view11)
}
}
如果我们删除" required"我们得到这个错误:
"fix-it Insert \n required initializer.
"
之后,如果我们运行,则抛出线程错误。
答案 0 :(得分:3)
课外做:
var imageView: UIImageView = UIImageView()
然后你就可以全局使用它了
答案 1 :(得分:1)
您可以将fatalError
代码替换为super.init(coder: aDecoder)
来消除错误。
我认为,从笔尖(或故事板)初始化的对象需要进行初始化。