UITableViewController save issue - objective-c

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.

Related

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];

get each cell textView data when click on button

I have a UITableView with UITextView in all the cells. I do have a bar button .On editing any of the UITextView and when I tap on the bar button all the UITextViews data must be stored in the form of NSArray of dictionaries. I will be thank full to you ,If I get out of this problem
Don't wait for your bar button to be pushed. If the table view is scrolled and the cells go off screen then any text entered by the user and not already saved will be lost.
Instead, your controller should be the delegate for all of the text fields. Whenever the text changes, save the change (probably using textField:shouldChangeCharactersInRange:replacementString:) into a temporary array / dict. Now, when configuring your cells, enter the text from the temporary store into the cell text fields and when the bar button is pressed use your temporary store to save the entered text.
To get the text to save, assuming that the text field is tagged with the array index and that the array contains mutable dictionaries (you should consider adding some protection and error logging):
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *stringToSave = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSLog(#"Save me: %#", stringToSave);
NSMutableDictionary *storeDict = [self.storeArray objectAtIndex:textField.tag];
[storeDict setObject:stringToSave forKey:#"description"];
return YES;
}

Enabling Done Button When User Changes Values - Xcode

I am trying to make it to where if any value is typed on the keyboard without hitting the return key and two of the labels have been changed from "Select" to something else, the button up at the top becomes enabled. However, I have tried using an IBAction saying:
- (IBAction)valuesChanged {
if (textField.text != nil && ![labelOne.text isEqualToString:#"Select"] && ![labelTwo.text isEqualToString:#"Select"]) {
NSLog(#"Success");
}
else {
NSLog(#"No Success");
}
}
But I have realized that this does not work because:
The textfield does not work when I put the IBAction Sent Event as "Value Changed"
The labels won't accept an action.
How do I go about doing this?
The text field delegate method that tells you that the user is typing a character in the text field or otherwise changing its contents is textField:shouldChangeCharactersInRange:replacementString:. Implement it in your text field delegate and respond as appropriate. You will also just return YES.
One reason your original code couldn't be hooked up might be that you have not used the canonical form of an IBAction method; it should be
- (IBAction)valuesChanged:(id)sender {
Another problem in your original code is that a UITextField does not emit Value Changed. What you wanted was Editing Changed. But the delegate method works just as well.
Try UITextField delegate methods.
- (void)textFieldDidBeginEditing:(UITextField *)textField;
- (void)textFieldDidEndEditing:(UITextField *)textField;

How to change value of a UITextfield on a UIButton click?

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.

How to use "takeStringValueFrom" with Button onclick Event?

I have a button that correctly wired such that when it is clicked, the "login" IBAction is hit.
- (IBAction) login: (id)sender
{
NSLog(#"OK");
}
Now, I wired the button to the username/password text fields using "takeStringValueFrom" but I don't understand how to then get at those values?
Hope that makes sense.
EDIT: so basically, when i click the "login" button the above event fires. I'd like to grab the values from two text boxes on that same window, what is the best way to accomplish this? I suppose I could use IBOutlet for each of the text boxes ... is this the correct way?
Re-reading the doc, it's possible "takeStringValueFrom" isn't what i thought it was.
You want to declare your username and password textfields as IBOutlets, and then hook them up in Interface Builder. Then, in your login handler, you use the stringValue message to extract their values:
- (IBAction) login: (id)sender
{
NSString *username = [usernameTextField stringValue];
NSString *password = [passwordTextField stringValue];
// check username & password
}