Calculator application, pupulating textfield from buttons, iphone - iphone

I am creating a simple calculator application. I have 10 buttons labelled 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.

Related

How can I tell which button was pressed LAST - SWIFT?

Pretty basic - I have multiple buttons and I just need to tell which button was pressed most recently, despite how many times on each or how long between clicks.
Give each button a unique tag in Interface Builder.
Make a variable that holds the tag of the last button that has been clicked.
In event handlers of your buttons set the variable from the step above to the tag of the button currently being clicked.
When you need to find the last button that has been clicked, check the variable, it will have the unique tag of your button.

how to recognize which key is pressed custom keypad and which text field is selected

i need to add custom keypad in app.
for that i add the images on buttons like this.
i have 10 textfields in my app.
when ever i press the keys i need to print on selected textfield.
How can i recognize which button is clicked,which textfield is selected.
can any one pls post some code.
Thank u in advance.
Set the tag property of each textfield to a different value. Pass in the textfield as the sender argument to the IBAction. Check the tag of the argument.

Adding a button to last UITableView row

I'm developing an app where the user can choose between a number of included songs. I also want the user to be able to choose a song from his/her iPod Library.
Currently the song is choosen by selecting it in a UITableView. So I figure I would like to add a new row at the end of the table and make it a button that will fire a MPMediaPickerController. All songs are placed in an array consisting of their names.
My question is how I add this last row? And also how I can "save" the selected song (or the path to it) to be used in the parent viewcontroller?
Well, you can use the UITableViewCell directly as a button itself, so when a user clicks on the last row, the action is being executed. But if I understand you right, you want to add a specific extra button as a subview of a UITableViewCell. That means if the cell (the last row) is being built, you compose your button, add its target and action, and add the button just as simple subview of the cell.
Well, the parent should now receive the message that the button has been pushed. I would do this using NSNotification, that is very easy to use, just check out the Apple documentation and take a look at an example. You can even send the selected song or its name path via the notification directly to the parent controller, where you can handle this notification.

UIPickerView row selection

I have a UIPickerView with two components.
It works fine when the user scrolls each component until it reaches the desired value.
But I want it to behave like the picker in the calendar or the clock apps. Meaning: When the user presses a certain value in one of the components, I want that component to automatically turn that row to be the selected row (so the user doesn't always have to scroll, he/she can also simply select the value they want).
Does anyone know how to do that?
Thank you,
~Chonch
It is standard picker behaviour and it should work so automatically.
If your picker does not select tapped row automatically try to set userInteractionEnabled property to NO for the view you return from viewForRow: method in picker data source.

Detect what's being inputed in a UITextField

I have a UITable View with a textfield that is editable right on the view (like Phone in contacts, etc.). I want to enable/disable my save button conditional up text being present in this field. So, I want the button to start out as disabled (for a new record) and then, as soon as I type the first letter into my text field, I want the button enabled. If I delete again back to zero, I would like the button disabled. You get the point.
Now, for doing this I need some way to detect the text being inputed while the user writes it (and when he finishes editing).
Does anybody know how to do this?
Thanks a lot. Still noob...
Try this: (from the Apple documentation for UITextInputTraits)
enablesReturnKeyAutomatically
A Boolean value indicating whether the return key is automatically enabled when text is entered by the user.
#property(nonatomic) BOOL enablesReturnKeyAutomatically
Discussion
The default value for this property is NO. If you set it to YES, the keyboard disables the return key when the text entry area contains no text. As soon as the user enters any text, the return key is automatically enabled.
Have your view controller adopt the UITextFieldDelegate protocol, and then implement a couple of the protocol's methods:
– textFieldDidBeginEditing:
– textFieldDidEndEditing:
Also, be sure to set the text field's delegate property to point to your view controller. The text field will then automatically send these messages to the controller when editing session begins and ends.