Xcode: Why doesn't this code work for saving UITextField using NSUserDefaults?
I have an UITextField inside an UITableViewCell. What I want't is for the app to save the text the user inputs. I've tried by making the UITextField call a action with this code:
[TextField addTarget:self action:@selector(saveTextField:) forControlEvents:UIControlEventEditingDidEndOnExit];
However this action that it load doesn't work correctly:
- (IBAction)saveTextField:(id)sender {
NSString *TextFieldString = [[NSString all开发者_运维知识库oc] initWithString:TextField.text];
[TextField setText:TextFieldString];
NSUserDefaults *UserSettings = [NSUserDefaults standardUserDefaults];
[UserSettings setObject:TextField forKey:@"TextField"]; }
When I exit the UITextField by trying to hide the keyboard by clicking "Done" I get this message:
*** WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: <NSInvalidArgumentException> *** -[NSPlaceholderString initWithString:]: nil argument
and nothing happens in the app.
Any thoughts?
Thanks in advance :)
I use the exact same functionality in one of my apps, and this is the code I use:
[[NSUserDefaults standardUserDefaults]setObject: textField.text forKey:@"TextField"];
Basically, I don't think the first two lines in your saveTextField method where you alloc a new string are necessary.
To begin with: thanks for your effort and time :)
Turns out the problem wasn't with the code that saves and loads it. The problem was that I only created one UITextfield which I then put into every UITableViewCell and that was the problem. So I created individual UITextFields for every UITableViewCells and that fixed the problem :)
精彩评论