iphone UITextField remembering user's previous input? [duplicate] - iphone

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
iPhone inputting NSUserDefaults into a UITextField
Is there a way to remember what the user put into a UITextField and have it displayed the next time they come to that UITextField? i.e. - have them input their name the first time they come to the "Name" UITextField but have that name already displayed in that field the next time they come across that UITextField?
I want the name to still be editable if they come back to the UITextField, but inputted nonetheless in case they don't need to change it the second time around.

If you want the UITextField to keep it's data between launches of your app, you will need to look into data persistence.
If you just want it to be the same if they navigate back to it from another view, simply leave the UITextField allocated, or better yet, store the data in an object reserved for data storage, not presentation, and then load the existing name from that every time you allocate the UITextView.

iPhone inputting NSUserDefaults into a UITextField

Related

Get Text Input WITHOUT UITextField Swift

I'm making an app and I want the user to be able to input text without having them actually click on a UITextField (have it open after the game ends for a scoreboard), is there some way to do that?
Call the following in the viewDidLoad of scoreboard, or whatever the entry point of that view is.
yourTextField.becomeFirstResponder()
Yes, just:
<#your_text_field#>.becomeFirstResponder()
after the game ends.
This will produce the "same" effect as a tap on the UITextField. If this is on a new UIViewController you can place it on viewDidAppear, otherwise you can assume everything is already loaded and simply call it anywhere.

swift update textview after typing new text

I'm making an app in swift 3
I have a TableViewController, when I click on a row it opens a ViewController in which there is a TextView. When I click on it, I am able to edit it, but the changes are not "saved". when I go back to my list and re-click on the same row, the text is back to "Default", I've been on this problem all day, and I don't know how to solve it, I've tried solution from stackoverflow but only give solution to change the text colours.
So how can I do that ?
Based on what you've said so far, I can only assume that you are not storing the new text that's been entered into your UITextView.
The reason that the changes are not being saved is that every time you "open" a new ViewController by tapping on a TableViewCell, it is a brand new instance of that ViewController. That means that it has no input as far as what should be displayed in the TextView, and therefore displays the default text.
But how do I fix it?
This is going to require that you save your TextView's text every time that you leave or dismiss the ViewController. This is because when you leave the ViewController, it is being released from memory. This means that anything that was written there no longer exists. I would suggest storing the text in an array of strings on your TableViewController, which should allow for the text to persist over the life cycle of the app.
The text disappears when I close the app!
This is something that will require data persistence, and for that I would suggest reading up on how to use Core Data to store and persist data across multiple app life cycles.
There are many ways to achieve that, the idea is to set the text of the text view in its view controller's initialization.
So, you need a place to save the data entered by the user, and then read that data again in the view controller's initialization.
One way to do that is using a singleton class to save the data, like this for example:
class SharedData {
static let instance = SharedData()
var textEnteredByUser: String?
}
Now, when the user enters text, save that text in the string we have in 'SharedData' like this:
SharedData.instance.textEnteredByUser = textView.text!
Now, we have the data entered by the user saved, we need to set the data in the text view before it appears in the screen, and this can be achieved inside viewDidLoad method of your view controller like this:
override func viewDidLoad() {
super.viewDidLoad()
textView.text = SharedData.instance.textEnteredByUser
}
You can also save your data in user defaults instead of the singleton class like this:
UserDefaults.standard.set(textView.text!, forKey: "textEnteredByUser")
And then retrieve the saved text this way:
UserDefaults.standard.string(forKey: "textEnteredByUser")
Note: The singleton solution keeps the data shared between different view controllers, but does not persist data after closing the application, and this is what user default does.

UIDatePicker change event not firing on first spin Swift [duplicate]

This question already has answers here:
Objective-C: UIDatePicker UIControlEventValueChanged only fired on second selection
(8 answers)
Closed 5 years ago.
I am trying to update a label when the UIDatePicker (Count Down Timer) is updated by the user. However, it only updates it after the first spin. The first spin does not change the label value. I have attempted to perform the UI update on the main queue to no avail.
This is a well-known and long-standing bug. If you look closely at Apple's projects that use a count down date picker (like the Timer part of the Clock app), they never try to respond to the action of spinning the picker; the user has to tap a button which then reads the value of the picker. I suggest you design your interface similarly.

Diffrence between textfieldshouldendediting and textfieldDidendediting in iPhone [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Diffrence between textfieldshouldendediting and textfieldDidendediting in iPhone
What is the diffrence between textFieldShouldendEditing and textfieldDidEndEditing methods? and when will use these methods.
From documentation:
textFieldShouldendEditing:
This method is called when the text field is asked to resign the first responder status. This might occur when your application asks the text field to resign focus or when the user tries to change the editing focus to another control. Before the focus actually changes, however, the text field calls this method to give your delegate a chance to decide whether it should.
textfieldDidEndEditing:
This method is called after the text field resigns its first responder status. You can use this method to update your delegate’s state information. For example, you might use this method to hide overlay views that should be visible only while editing.
So, the textFieldShouldendEditing:method will be called before textfieldDidEndEditing:method
textFieldShouldendEditing will call when your textField is about to end edit mode. It has a BOOL return type. If you set return NO than your textField will not be resignFirstResponder and remain in editing mode and textfieldDidEndEditing is not going to call.
while textfieldDidEndEditing this is method which tell you that your text field not in editing mode and your keyboard is down.
For more details plz refer UITextFieldDelegate Protocol
Hope this helps :)
textFieldDidEndEditing:
is called when the text field resigns as the first responder. So you can then get the value entered from the user.
textFieldShouldendEditing: is used to check if the key entered by the user should be displayed on the text field or not. So if you return NO the key is not displayed.
Imagine a label where you want to restrict the number of letters entered. You can use the above to check how many characters are entered and if they have reached the limit, return the value NO which basically doesn't take any new key strokes.
You can check the reference doc for more info.

Xcode iPhone - Class that asks for multiple text inputs and then returns them

I wan't to creata a "CatchNames" class which I can import into a view Controller that shows a text which asks for text input. I would like to be able to add an instance of CatchNames to my view, have it ask the user for three names in a row and return them in an array.
[self.view addSubview:[catchNames view]];
NSArray *myNamesArray = [catchNames namesArray];
The best way would be to have the application freeze kind of the way it does when you are prompted to enter a password in iOS and continue when the user entered 3 names so I can immediately catch the array in the next line.
While this might not be the best description I still hope you understand my problem.
How can I approach this?
Thank you in advance
I guess you appear to be looking to implement a simple form which gets the user input, retrieves and stores it in an array? Hopefully I haven't misunderstood the question, but this seems to be a simple task you can accomplish with one or more UITextField's and a UIButton as a 'Add' or 'Done' call to action.
Are you looking for some general UI coding level help regarding implementing such a view? If so, I would encourage taking a look at the XCode documentations of UITextField (for capturing text), UIButton (for handling actions) and UIView (for view hierarchy and animation implementation).
Some quick notes;
Looks like 3 names are compulsory, so, you may verify whether a UITextField is empty at the button's click action.
Have the array declared in the view controller, not the view
The 'freezing' you require should take care of itself as long as the view offers no other way out for the user other than clicking the button.
Do excuse me if I am oversimplifying the problem. Let me know if you need me to drill down into anything further.
Cheers!