Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

swift - Call to IBOutlet in custom UIView from another class

I am looking for a way to refer to the IBOutlet variable in a custom class UIView, from another class. I found layoutSubviews, but each change only works on the first call, and not on each subsequent call. Thanks for help!

ViewController class:

var SB = StatusBar()
SB.update(1)
SB.update(2)
SB.update(3)

StatusBar class:
class StatusBar: UIView {

    @IBOutlet var view: UIView!
    @IBOutlet weak var label: UILabel!
     
    var ActualStatus: Int!

    required init?(coder: NSCoder) {

        super.init(coder: coder)
        xibSetup()
       
    
    }
    
    override init(frame: CGRect) {

        super.init(frame: frame)
        xibSetup()
     
    }
     

    func update(status) {
        ActualStatus = status
        self.layoutSubviews()
    }

 
    override func layoutSubviews() {
           super.layoutSubviews()
 
        label.text = ActualStatus

    }


    func xibSetup() {
        
        
        view = loadViewFromNib()
        view.frame = bounds
        view.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth, UIView.AutoresizingMask.flexibleHeight]
        addSubview(view)
        
        
    }
    
    func loadViewFromNib() -> UIView {
        let bundle = Bundle(for: type(of:self))
        let nib = UINib(nibName: "StatusBar", bundle: bundle)
        
   
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
        return view
    }
}

Result: label.text is 1, change only works on the first call

question from:https://stackoverflow.com/questions/65642590/call-to-iboutlet-in-custom-uiview-from-another-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...