How to change value of a UITextfield on a UIButton click? - objective-c

I have two text fields and a button on my view.
In the first text field, user enters some value in celsius and by clicking on the button, user is able to find the fahrenheit value on the other textfield. This works fine.
However, when user enters another celsius value in the first textfield, at the touch of the button the fahrenheit value is APPENDED to the previous fahrenheit value, instead of changed.

in your button action function
farenheitTextField.text=nil;
[farenheitTextField setClearsOnBeginEditing:YES];
farenheitTextField.text=#"your calculated value";

Try with this...u need to call this method
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
return YES;
}
Also u need to set the previous value of ur 2nd textfield to nil in the Button Action.

On button click event just write text2.text = #"";
and after that write your entire code.

Use the delegates function of textfield and just assign a empty NSString to your NSString that contains the value of the first textfield.
As in: nsstring *demoStr = #"";
Now it will pick the fresh value each time .

Try changing the value of the textField to #"" before you assign the new value. All this on the IBAction method.

You may want to try this on button click event:
textField.text = #"YourString";
Hope this helps.

Related

TextField on Custom Cells

I am using custom cell inside tableView. In that custom cell, there is label and textField....this custom cell is being reused to display ten items. I actually want to open a datepicker on one of the cell's text field but I am not able to understand that how this work can be achieved?
All you need to do is to create an instance of UIDatePicker and set it as inputView of one of your text fields. When the text field is tapped UIKit will animate appearance of the picker for you. In turn, you'll have to handle user's input and update the text field's text.
Please set the text field tag and delegate in cellForRowAtIndexPath method...
like..
cell.yourTextFieldName.tag = 1000;
cell.yourTextFieldName.delegate = self;
In .h file add the UITextFieldDelegate
Then in the textFieldShouldBeginEditing delegate you can get the picker.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSInteger txtFldTag = textField.tag;
if(txtFldTag == 1000){
//write the method to show picker.
return NO;
}

How can I make my NSTextField NOT highlight its text when the application starts?

When my application launches, the first NSTextField is being selected like this:
I can edit the NSTextField fine, but when I press enter to end the editing, the text becomes selected again, and the editing does not end.
I followed the Apple tutorial here, and I had the same problem with the text field being perpetually highlighted.
How do I stop this? I would like it so the text field is not the first responder of the app so it's not edited right away, and when it is being edited, clicking outside of the text field will end it. I'm not sure where to put the [[textField window]makeFirstResponder:nil] to stop the editing in the latter case.
I'm running Yosemite 10.10.2.
Your text field is selecting the text, due to the default implementation of becomeFirstResponder in NSTextField.
To prevent selection, subclass NSTextField, and override becomeFirstResponder to deselect any text:
- (BOOL) becomeFirstResponder
{
BOOL responderStatus = [super becomeFirstResponder];
NSRange selectionRange = [[self currentEditor] selectedRange];
[[self currentEditor] setSelectedRange:NSMakeRange(selectionRange.length,0)];
return responderStatus;
}
The resulting behavior is that the field does not select the text when it gets the focus.
To make nothing the first responder, call makeFirstResponder:nil after your application finishes launching. I like to subclass NSObject to define doInitWithContentView:(NSView *)contentView, and call it from my NSApplicationDelegate. In the code below, _window is an IBOutlet:
- (void) applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
[_menuController doInitWithContentView:_window.contentView];
}
The reason your field is getting focus when the application starts is because the window automatically gives focus to the first control. It determines what is considered first, by scanning left to right, top down (it scans left to right first, since a text field placed at the top right will still get focused). One caveat is that if the window is restorable, and you terminate the application from within Xcode, then whatever field was last focused will retain the focus state from the last execution.
I am using IB, there's a property on NSTextField called Refuses First Responder. Ticking that will prevent the highlighting of the text field immediately after the window is presented. There's some more detailed info about Refuses First Responder in this question.
No need to subclass. Simply set refusesFirstResponder = YES;
NSTextField *textField = [NSTextField new];
textField.refusesFirstResponder = YES;
That's it! Do that and it won't highlight the text in the field.

Objective-C, auto erase NSTextField string

I've been trying to figure out how to auto erase a string within an NSTextField upon clicking within in it.
For example the current behavior is like this on OSX:
When a user clicks in the cell or the default focus is the cell either the word is sometimes highlighted or the cursor is placed in the middle of the text. The user then needs to use backspace to delete the text (which I'd like to eliminate by automatically removing the text).
Is this a function that is built-in that I'm somehow missing? If not how would I go about it?
As far as I know there is no standard function to delete the text that is in the textfield. Depending on your controller class you can try to work with mouseDown or acceptFirstResponder, but I don't believe this will provide a stable situation.
I would recommend you to start with an empty textfield (setting in xcode) and empty the textfield after the content of the textfield has been processed. For instance:
-(IBAction)addYourTextFieldInWhateverYouWant:(id)sender
{
//copying the string from the textfield
NSString *text = [[self yourTextField]stringValue];
//now you can empty the textfield
[[self yourTextField]setStringValue:#""]
//Do here whatever you need to do with the input from the textfield.
....
}
It doesn't give you exactly what you want, but it provides the user with an empty textfield where the user can immediately type in his/her text when the textfield is clicked upon and the user doesn't need to delete any text in the textfield.
Hope this helps.
Kind regards,
MacUserT
There are plenty of terrible ideas “outside the box”, and this is one of them.
Anyway, make a subclass of NSTextField. Override becomeFirstResponder. If [super becomeFirstResponder] returns YES, set self.stringValue = #"".

How to call a method when values has been entered in text box

In my app, i have a text box and handheld scanner. when i scanned something, the barcode values comes in text box. I don't need any special code to get the bar code value inside the text box. Till here i am fine. But now when i have a value in textbox , i want to call a method which will further do some changes in Core data. I have the code to make changes into core data for the row with value = value in text box.
But i don't know how to call this method or how do i know that values been entered in text box and i can call the method.
Please help me in doing that.
Sometimes handheld device add return character to the end and if yours do that add delegate to the text field
self.textField.delegate = self;
and see is that method called:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(#"textFieldDidEndEditing method called");
}
If this method is not called add notification to the text field which will be called when the text changed:
[self.textField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
and add method which will be called:
-(void)textFieldDidChange(id)sender
{
NSLog(#"textFieldDidChange method called");
}
You need to create an IBOutlet variable and link it to your textbox.
#interface YourClassOrController
{
IBOutlet NSTextField *myTextField;
}
...
#end
In interface Builder, you need to Ctrl-Drag to link your textfield to your variable.
Finally in your code you use
NSString code = [myTextField stringValue];

UITableViewController save issue

I've a UITableViewController which has Edit/Save and cancel buttons. At present I'm displaying 5 row's of custom UITableViewCell's. Custom UITableViewCell contains textfield and label. Let's say if user enters text in 1st table cell and hits "Save" then I'm losing the entered/modified text. Whereas if user hits return key after entering/modifying text then the entered/modified value is captured via:
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
.......code exists to capture value here
}
but on the other hand if user hits "Save" button without returning the textfield then I'm losing the entered/modified value. What's the best way to capture user's input here:
a) when user returns text field --> which I'm doing already
b) when user hits save button without returning textfield/textview --> ????
thanks in advance,
Rama
You could keep a property that holds the string as it's being entered using a delegate method. Something like:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSMutableString *testString = [NSMutableString stringWithString:self.textField.text];
[testString replaceCharactersInRange:range withString:string];
self.stringInProgress = testString;
return YES;
}
Another option might be to call [tableView endEditing] as the first thing to do on a save but I'm not certain that fires the textFieldShouldEndEditing: notification.