mercredi 22 juin 2016

iOS custom view - very weird properties behavior


Using Swift 2.0

I'm trying to use a custom view with a label (not just that) to show some information on the screen, so my idea was to programatically add the custom view to a scrollview on the ViewController, and set the label's text (to start with)

Problem: I just can't get a moment where both the label and the string i want to set the label's text to are not null, and for some reason the string gets null in the end.

So i made a extensive debugging to show what's the state for each var in each moment, and that made me even more confused: (this is Xcode's log output)

my init was called, fatura String value: Optional("random text")
init with coder was called

 - On awake from Nib
Label outlet is NOT null
fatura String is null
view loaded from Nib was added

 - On Setup
Label outlet is null
fatura String is NOT null
checking fatura String value from ViewController: Optional("random text")

 - On ViewController's viewDidLoad
Label outlet is null
fatura String is NOT null

 - On ViewController's viewWillAppear
Label outlet is null
fatura String is NOT null

 - On Button touched action
Label outlet is NOT null
fatura String is null

I'm new to iOS, coming from some years in android development,and I would like to really understand what's going on, so any good articles and books would be welcome as well. I've found a lot of stuff on custom views, but they all are somewhat different , so I don't know which one to use, and they're mostly implementations only, no explanation whatsoever.

Questions:

  • Why does the vencimentoLb Label's outlet starts out as "not null", before the Nib view was loaded?

  • Why is the fatura String null, after* my init was called, which sets it out? *at least that's what the log tells me

  • Why the hell is my fatura String null on the buttonClicked action??

To Summarize: how can i get both the label and the string not null, so to set the text on the label?

I know it's a lot of questions, so an answer for any of them is much appreciated

"Solution" found:

call customview.view to force the outlets to get instanciated. that did not work (the customview has no view member apart from the one I created, and calling that one made no difference)

Custom View's code:

class FaturaCard: UIView {

    var view:UIView!
    @IBOutlet var vencimentoLb: UILabel!
    @IBOutlet var cardView: UIView!

    var fatura:String?

    init(string:String , frame:CGRect){
        fatura = string
        print("my init was called, fatura String value:",self.fatura)
        super.init(frame: frame)
        setup()
    }

    override init(frame: CGRect){
        super.init(frame:frame)
        print("init with frame was called")
        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        print("init with coder was called")
    }

    @IBAction func onBotaoClicked(sender: AnyObject) {
        print("n - On Button touched action")
        checksWhatsNull()
    }

    func setup(){
        view = loadViewFromNib()
        view.frame = bounds
        view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
        addSubview(view)
        print("view loaded from Nib was added")
        print("n - On Setup")
        checksWhatsNull()
    }

    func checksWhatsNull(){
        if vencimentoLb == nil{
            print("Label outlet is null")
        }else{
            print("Label outlet is NOT null")
        }

        if self.fatura == nil{
            print("fatura String is null")
        }else{
            print("fatura String is NOT null")
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        print("n - On awake from Nib")
        checksWhatsNull()
    }

    func loadViewFromNib() -> UIView{
        let nib = UINib(nibName: "FaturaCard", bundle: nil)
        let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
        return view
    }

}

Edit:

Custom view instantiation code:

var view2:FaturaCard!
override func viewDidLoad() {
        super.viewDidLoad()

        view2 = FaturaCard(string:"random text",frame: CGRect(x: 00, y:145, width: scrollView.bounds.size.width, height: 145))

        print("checking fatura String value from ViewController:",view2.fatura)

        self.scrollView.addSubview(view2)

        print("n - On ViewController's viewDidLoad")
        view2.checksWhatsNull()

        self.scrollView.contentSize.height=145*2
    }

Aucun commentaire:

Enregistrer un commentaire