i have text field in view controller , want display custom title button. represent changes made in text field.
override func viewdidload() { super.viewdidload() // update button in nav bar updatebackbutton() // text field delegation nametextfield.delegate = self nametextfield.addtarget(self, action: "updatebackbutton", forcontrolevents: .editingchanged) } func updatebackbutton() { let backbutton = uibarbuttonitem( title: formhaschanged ? "cancel" : "back", style: .done, target: nil, action: nil ) print(backbutton.title) navigationcontroller?.navigationbar.topitem?.backbarbuttonitem = backbutton } this effect button once, in viewdidload method. on subsequent calls updatebackbutton() there's no visible change, though print(backbutton.title) print appropriate output.
what's missing approach in order have dynamically updated button title?
output updatebackbutton() method's print statement.
optional("back") optional("cancel") optional("back") optional("cancel") optional("back")
if want call function action control's target you'll have define @ibaction , use : while calling it.
override func viewdidload() { super.viewdidload() // update button in nav bar // updatebackbutton() // don't need call here, guess.. // text field delegation nametextfield.delegate = self nametextfield.addtarget(self, action: "updatebackbutton:",forcontrolevents: .editingchanged) } @ibaction func updatebackbutton(sender: anyobject!) { let backbutton = uibarbuttonitem( title: formhaschanged ? "cancel" : "back", style: .done, target: nil, action: nil ) print(backbutton.title!) navigationcontroller?.navigationbar.topitem?.backbarbuttonitem = backbutton } update :
try changing line
navigationcontroller?.navigationbar.topitem?.backbarbuttonitem = backbutton navigationcontroller?.navigationbar.topitem?.leftbarbuttonitem = backbutton or navigationitem.leftbarbuttonitem = backbutton
ps : not show < symbol think dont need dealing cancel , done.
its working now. hope work !
Comments
Post a Comment