I am creating a simple calculator application. I have 10 buttons l开发者_运维技巧abelled 0 through 9, and 5 buttons for the operations. I have an uitextfied which should be populated from the buttons. ie when the user click button 1, 1 should be entered in the field and so on. Also the value entered should be there even if the user taps another one. how to do that..pls help..
You need to declare the textfield (and the buttons) as Outlets
in Interface Builder. Then you need to connect the touch up inside event of each button to a method. You can either have 10 methods which is one for each button or just one method and filter on id
or tag
. In the button actions you can easily update the textfield by appending the the correct number.
I would personally store user input in a NSMutableString, validating input (for numeracy)and adding it to the variable if validation returns true.
Then when a button is pressed it is simply a matter of updating the UITextField with the contents of the NSMutableString.
For the purpose of calculations, the string value can be converted to the appropriate numeric type when a calculation is required.
精彩评论