ios - Dynamic updating of label/button title in Swift -
i want update title
property of user interface element on ios using swift. in case uibarbutton, uilabel, uibutton or whatever. using code, works:
func setstatusmessage(barbutton: uibarbuttonitem) { let currentversion = statusmodel.getcurrentversion() var statusupdates = [statusmodel]() var statusforcurrentversion: statusmodel! var statusmessage = string() // check if update required before setting text checkiflocalstatusneedsupdate() barbutton.title = getlocalstatusmessage() // try update status anyway... getstatusfromremotesource { (statusupdates) -> void in status in statusupdates { if status.version == currentversion { statusforcurrentversion = status } } self.savestatusfromremotesource(statusforcurrentversion) barbutton.title = statusforcurrentversion.message } }
although effective, solution ugly require user interface (view) element embedded in model. not sort of mvc beauty.....
i cannot use return
because local status returned before remote status can fetched. guess need kind of handler / listener / delegate (/*getting lost here*/
) update view dynamically. in case means: set title using locally stored value , update if remote value received.
what best way approach scenario in mvc compliant way, removing ui elements model code (thereby increasing reusability)?
one intermediate step going on controller ui, replacing barbutton parameter reference controller
barbutton.title = statusforcurrentversion.message -> self.controller.update(title: statusforcurrentversion.message)
this way code allowed grow (updating more labels etc), comes cost of more code , harder readability.
Comments
Post a Comment