ios - UILabel text isn't updated when modal re-appears -


i've created reusable modal uiview singleton object, having trouble changing text of label contains. when modal appears second time, new text appears on old text. i'm guessing due fact label instantiated once? if so, why can add text not remove it? here's code:

class transparentmodal: uiview {  static let modal = transparentmodal()  class func modalinview(view: uiview, forselectionorplacement: string) -> transparentmodal {     let f = cgrectmake(0, view.bounds.size.height, view.bounds.size.width, view.bounds.size.height)     modal.frame = f     modal.alpha = 0.7     modal.backgroundcolor = uicolor.darkgraycolor()      let button = uibutton(frame: cgrectmake(0, 0, 116, 50))     button.center = cgpointmake(view.bounds.size.width / 2, view.bounds.size.height / 1.6)     button.backgroundcolor = uicolor.lightgraycolor()     button.layer.cornerradius = 4     button.settitle("ok", forstate: uicontrolstate.normal)     button.addtarget(self, action:"dismiss:", forcontrolevents: uicontrolevents.touchupinside)     modal.addsubview(button)      let label = uilabel(frame: cgrectmake(0, 0, 260, 100))     label.center = cgpointmake(view.bounds.size.width / 2, view.bounds.size.height / 3)     label.numberoflines = 0     label.text = ""      // i'm going wrong     if forselectionorplacement == "selection" {         label.text = "" // attempting remove previous text         label.text = "text condition one."     } else if forselectionorplacement == "placement" {         label.text = ""         label.text = "text condition two."     }     modal.addsubview(label)     view.addsubview(modal)      self.animatewithduration(0.5, animations: { () -> void in         self.modal.frame.origin.y -= view.bounds.size.height     })      return modal }  class func dismiss(sender: uibutton) {     self.animatewithduration(0.2, animations: { () -> void in         self.modal.alpha = 0     }) { (bool) -> void in         self.modal.removefromsuperview()     }   } } 

i know bit of convoluted way create simple modal. more exercise in creating reusable object. modal needs appear on other modal views, views no navigation controller, etc., wanted try make flexible.

update: below answer correct except variables can added static class. removing label in dismiss function allows label re-instantiated new text @ modal's next appearance.

as kursus wrote in comment: not remove button , label subviews parentview (the actual modal view). if presented again 2 new instances created , positioned on previous ones. guess see label because transparent, buttons overlay completely.

to fix add 2 variables class:

var button:uibutton! var label:uilabel! 

and change 2 lines in modalinview

if label == nil {     label = uilabel(frame: cgrectmake(0, 0, 260, 100)) } 

and

if button == nil {     button = uibutton(frame: cgrectmake(0, 0, 116, 50)) } 

that cause button , label created if have not been created before.

another way remove 2 views in success block of dismiss function like

self.animatewithduration(0.2, animations: { () -> void in     self.modal.alpha = 0 }) { (bool) -> void in     self.modal.removefromsuperview()     button.removefromsuperview()     label.removefromsuperview() } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -