1

I have witten a custom UIView class which takes several parameters and overrides an empty storyboard UIView(subclassed as MultiPositionTarget class instead of UIview) as below.The views below are linked as outlet to view controller as it is seen from the code below(in total 9 views).

enter image description here

// Initialize the targets
target1 = MultiPositionTarget(.zero,"targetTemp.png","David","Target")
target1.parentVC = self
targetList.append(target1)

However, it does not accept the parameters. And only loads the view. Here is my class below:

class MultiPositionTarget: UIView{

    var targetName: String!
    var bottomLabelName: String!
    var imageName: String!
    var isSelected: Bool!
    var labelTop: UILabel!
    var labelBottom: UILabel!
    var parentVC: SelectTargetViewController!

    init(_ frame: CGRect,_ imagName: String,_ targetname: String,_ targetlabel: String){

        self.imageName = imagName
        self.targetName = targetname
        self.bottomLabelName = targetlabel
        super.init(frame: frame)
        setUp()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUp()
    }

    func setUp(){

        let viewWidth = self.bounds.width
        let viewHeight = self.bounds.height
        // Add top label
        labelTop = UILabel(frame: CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight/5))

        //labelTop.center = CGPoint(x: 160, y: 285)
        labelTop.textAlignment = .center
        labelTop.font = UIFont(name:"System",size:6)
        labelTop.text = self.imageName
        labelTop.textColor = UIColor.white
        labelTop.backgroundColor = hexStringToUIColor(hex: "#770B2C")
        //labelTop.alpha = 0.5
        self.addSubview(labelTop)

        let image = UIImage(named: "targetTemp.png")
        let imageView = UIImageView(image: image!)
        imageView.frame = CGRect(x: 0, y: (viewHeight-viewHeight * 4/5), width: viewWidth, height: (viewHeight * 4/5))
        self.addSubview(imageView)

        // Add bottom Label
        labelBottom = UILabel(frame: CGRect(x: 0, y: (viewHeight * 4/5), width: viewWidth, height: viewHeight/5))

        //labelBottom.center = CGPoint(x: 160, y: 285)

        labelBottom.textAlignment = .center
        labelBottom.text = self.bottomLabelName
        labelBottom.font = UIFont(name:"System",size:10)
        labelBottom.textColor = UIColor.white
        labelBottom.backgroundColor = hexStringToUIColor(hex: "770B2C")
        labelBottom.alpha = 0.95
        self.addSubview(labelBottom)

        self.isUserInteractionEnabled = true
        let viewTap = UITapGestureRecognizer(target: self, action: #selector(singleTap))
        self.addGestureRecognizer(viewTap)

    }

}

Any help or hint is appreciated. Thanks in advance stack overflow family.

3
  • Where did you put the code that says // Initialize the targets? Is target1 the outlet? Why are you initialising an outlet? Commented Mar 14, 2020 at 12:07
  • I don't think you can have custom initialisers when adding views through the storyboard. You might have to loop over all the outlets you have and set the properties that way. Commented Mar 14, 2020 at 12:13
  • I suppose you are using outlet name target1 and then reassigning it with your custom view. Here your mistake is when you are initializing your outlet it breaks from storyboard and since your custom view is initialise with frame zero and main view is not subviewing it, custom view will not show at all. Here you can make a @IBDesignable class or fully make a custom view and subview it programmatically. Commented Mar 14, 2020 at 12:25

1 Answer 1

3

This line is creating a new view:

target1 = MultiPositionTarget(.zero,"targetTemp.png","David","Target")

The new view is unrelated to the view shown on the screen. The new view isn't actually on the screen at all, because you haven't added it to the view hierarchy. Even if you did, it's invisible because it has a frame of zero.

In short, you can't call your custom initialiser and also design your view in the storyboard. You can initialise the properties you want either in the storyboard as well, or in code.

If you want to initialise the properties in the storyboard as well, you can modify imageName, bottomLabelName and targetName with @IBInsepctable:

@IBInsepctable var targetName: String!
@IBInsepctable var bottomLabelName: String!
@IBInsepctable var imageName: String!
// or, if you want to directly select an image from the storyboard:
// @IBInsepctable var image: UIImage!

This way those properties will show up in the property inspector in the storyboard.

If you want to initialise it in code, you must set it with assignment statements, and not with an initialiser:

target1.targetName = "David"
target1.bottomLabelName = "Target"
target1.imageName = "targetTemp.png"
target1.parentVC = self
targetList.append(target1)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.