ios - How to call a function from a different function? -


i'm trying make simple app counts characters of textfield, when user inputs text, function converts user-inputed string var , function counts characters executed @ once. here's code:

import uikit  class viewcontroller: uiviewcontroller {      @iboutlet var mytextfield : uitextfield     @iboutlet var usertextfield : uitextfield      override func viewdidload() {         super.viewdidload()         // additional setup after loading view, typically nib.         mytextfield.text = fullconstant     }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }     @ibaction func modifymyvariable(sender : anyobject) {         myvariable = usertextfield.text     }     @ibaction func clickme(sender : anyobject) {         countcharacters(&fullconstant)         println(fullconstant)         mytextfield.text = fullconstant     } } 

and here's "otherfile.swift" functions located:

import foundation  var fullconstant = "type something!" var myvariable = ""  func modifymyvariable() {     println() }  func countcharacters(inout fullconstant: string) {     let firstpart = "there "     let lastpart = " characters"     var numberofcharacters = countelements(myvariable)     switch numberofcharacters {     case 0 :         fullconstant = "there isn't character yet"     case 1 :         fullconstant = "there 1 character"     default :         fullconstant = firstpart + string(numberofcharacters) + lastpart     } } 

both functions execute usertextfield edited, if user inputs 1 character, countcharacters function takes var myvariable before it's modified function modifymyvariable, doesn't count last character added.

to solve this, i've thought call function countcharacters function modifymyvariable, variable myvariable has changed when counts characters.

change following , see if it's easier fix problem.

  • you should link each event 1 ibaction. ibactions shouldn't named after you're trying in them; should named after event triggers them. example, "modifymyvariable" should called "textedited" or similar.
  • in "textedited" method, work need do. if need call function, call there instead of linking 2 ibactions.
  • put code in "otherfile" inside

    class otherfile { } 

block, , hold instance class variable in view controller. want avoid declaring global functions outside of classes.

  • not related, name constants using camelcase first letter lower case, variables. firstpart should firstpart.
  • avoid using 'inout' as possible. each language has it's conventions; in objc , swift, pass in values needed work, , return values result work. so:

    func countcharacters(text: string) -> (string) 
    • putting together, 'modifymyvariable' function (which should called 'textedited') this:

      myvariable = usertextfield.text let charactercount = self.myotherfileinstance.countcharacters(myvariable) mytextfield.text = charactercount 

and other function (clickme) should deleted.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -