简体   繁体   中英

how to declare UIView globally in swift?

Unable to create View globally. My code is :

 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)
    }
 }

If we erase that "required" line, we get this error:

"fix-it Insert \\n required initializer. "

after that, if we run, then the thread error is thrown.

Outside the class do:

var imageView: UIImageView = UIImageView()

Then you will be able to use it globally

You can get rid of the error by replacing the fatalError code with super.init(coder: aDecoder) .

This initialization is required for objects that are initialized from a nib (or storyboard), I believe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM