Getting text to save on iPhone - cocoa-touch

I am making an app the iPhone. I have a text field the user can type into and it's working, but I want it so you can press a button and it saves the text so when you close and open the app again it is still there. I have the button ("Done!"), but I don't know how to program it.

Look into the NSUserDefaults class for easy ways to save simple data.
And to get the "done" button on the keyboard to respond, you need to implement the UITextFieldDelegate (or UITextViewDelegate) protocol and override the textFieldShouldReturn: method.
In that method write something like:
- (bool)textViewShouldReturn:(UITextView *)textView
{
[textField resignFirstResponder];
//code to save text to NSUserDefaults
return yes;
}

Related

How to enable the next textfield in Objective C

I have two textfields (a username and a password) field and I remember that sometimes the keyboard has a little < or > to navigate through the textfields and I was wondering if there is an option or code that would allow me to do this natively in my app. Any tips or hints are appreciated. Thanks!
If you would like an acceptable solution that doesn't involve implementing a third party library, you can do it like the following:
Set the return button on the virtual keyboard to be a "Next" button for the username field, and a "Go" button for the password field, and make your view controller a UITextFieldDelegate. Set your view controller as the delegate for both text fields, then implement the textFieldShouldReturn: like the following:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.username) {
[self.password becomeFirstResponder];
}
else if (textField == self.password) {
[self.password resignFirstResponder];
[self signInPressed];
}
return YES;
}
This will make it so if you have the username field selected, the "Next" button on the keyboard will advance to the password field, and when the password field is selected, the "Go" button will call the signInPressed method, which you just change to whatever you have your sign in method named.
There's no builtin way to add these buttons.
Look into inputAccessoryView to understand how to extend the keyboard.

Click textfield and button disappears (it doesn't, but i want it to)

I have a textfield and a button. When I click inside the textfield, I want the button to disappear. I defined the textfield as both outlet and action ( with event “Did end on exit”). In the method for the textfield, I have self.testButton.hidden = YES; When I click inside the textfield, the button does not go away. Instead, it remains until I hit the return key on the keyboard – causing the keyboard to go away. I tried the same thing w/ touchup inside as the event on the text field. When you click in the text field, nothing happens to the button.
Instead of using the Target-Action mechanism ("Did end on exit" and "Touch Up Inside") use the Delegate mechanism.
First, make your class conform to the UITextFieldDelegate protocol. In your *.h (header) file add the following:
// Here I'm assuming your class is inheriting from UIViewcontroller but it
// may be inheriting from some other class. The really important part here
// is: <UITextFieldDelegate>. That's how you make your class conform to that protocol
#interface THE_NAME_OF_YOUR_CLASS : UIViewController <UITextFieldDelegate>
.
Second, implement the -(void)textFieldDidBeginEditing:(UITextField *)textField method. Also, remember to set yourself as the delegate too: self.textField.delegate = self. That way, the method will get called every time the user starts editing. Inside that methdod call self.testButton.hidden = YES;. In your *.m (implementation) file add the following:
-(void)viewDidLoad {
// here I'm assuming you have a 'strong' reference to your text field.
// You're going to need one to set yourself as the delegate.
self.textField.delegate = self;
}
// This is one of the methods defined in the UITextFieldDelegate protocol
-(void)textFieldDidBeginEditing:(UITextField *)textField {
self.testButton.hidden = YES;
}
.
Similarly, to make your button appear again, implement the - (void)textFieldDidEndEditing:(UITextField *)textField method. Inside it un-hide your button. Again, in your *.m file add the following:
// This is another method defined in the UITextFieldDelegate protocol
-(void)textFieldDidEndEditing:(UITextField *)textField {
self.testButton.hidden = NO;
}
Although delegates may be a mystery to you right now once you become familiar with them
you will realize they're very easy. And this is very important because iOS programming
relies heavily on delegates.
A delegate is a "notification" mechanism based on the "Hollywood" principle which is: don't call us; we'll call you.
In your case the class that contains the UITextField is interested in knowing when the UITextField begins editing and when it ends editing. But your class cannot be "polling" (that is, constantly asking) the text field to find out if the state changed. Instead you register your class with the text field and it will be the text field the one that will let you know when something happened. That will be thanks to the methods that you implemented.
Further reading: protocols and delegates
Hope this helps!
Have you made sure that testButton has its IBOutlet set before you hide it?
If you want to button to disappear when the user begins editing the text field, try UIControlEventEditingDidBegin.

Xcode - setFocus on a text field, becomeFirstResponder isn't enough

At the moment, I trigger a method on 'Did End On Exit' in my app (I'm aware that this may not be the greatest way of doing it but I'm very new to Objective C and Xcode for that matter and I'm simply doing what feels comfortable to me).
This method resigns the firstResponder from the current text field and applies it to a later text field.
The problem I'm facing is that the keyboard covers the next text field so that the use has no idea where the focus is and therefore what they are required to type.
How do I get it so that my keyboard shifts down and actually shows the text box that is currently active? Making something the firstResponder simply doesn't do what I want it to, unless there's part of the implementation I'm missing.
Here's my simple method:
- (IBAction)firstNameNext:(id)sender {
[firstNameTextField resignFirstResponder];
[surnameTextField becomeFirstResponder];
}
Any advice would be super.
Add UIScrollView in your main view then all contents as subview to UIScrollView
Now when specific UITextField needs to be able to visible in view use its delegate like this:
Note: add UITextFieldDelegate in .h file like this
#interface yourViewController : UIViewController<UITextFieldDelegate>
Also bind with File's Owner
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
{
if(textField == yourSpecficTextField) //one u want move upwards
{
yourScrollView.contentOffset = CGPointMake(0,200); //required offset
}
... //provide contentOffSet those who needed
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
yourScrollView.contentOffset = CGPointMake(0,0); //make UIScrollView as it was before
}
If you have keyboard input fields that will be covered by the virtual keyboard, then you need to move those fields out from under the virtual keyboard.
The normal way to do this is to have the controller's view be a scrollable view like UIScrollView. Moving Content That Is Located Under the Keyboard gives a very robust way of adjusting your scroll view and ensuring the required field shows.

How to turn off TextDidChange Event in iOS?

Now i am creating dictionary application in iOS.
In my apps, I used textDidChange Event of UISearchBar with UISearchDisplayController for Auto Complete searching.
So when i type a word in SearchBar,it's automatically search in database.
However i have a function in Preferences to turn off AutoComplete searching.
After i turn off AutoComplete function in Preferences, It's still auto complete searching with TextDidChange Event.
I don't want TextDidChange event this time.I want to search after i enter complete word in search bar and then click Search button from Keyboard.
So how can i turn off TextDidChange event from UISearchBar?
Please let me know if you ok.
Best Regards,
In your situtation you don't want to suppress the textDidChange event. Instead you want your UISearchDisplayDelegate's shouldReloadTableForSearchString: method to return NO unless the Search button has been clicked.
You could create a boolean ivar to keep track of this. Set the ivar to NO when textDidChange is called, and set it to YES when searchBarSearchButtonClicked: is called. Then in your shouldReloadTableForSearchString: you can do the test.
EDIT: Clarified by expanding code example. (In this example I am assuming you have set up a ivar named isSearchButtonClicked).
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
isSearchButtonClicked = NO;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
isSearchButtonClicked = YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
// returns YES if search button was clicked, otherwise return NO
return isSearchButtonClicked;
}
you can always create a bool variable and check if auto complete is on or not... based upon that you can search or not.
- (void)filterContentForSearchText:(NSString*)searchText
scope:(NSString*)scope
{
//create a bool variable and check is Auto Complete searching is Yes or No
if YES then pass searchText
if no then Don't pass any thing unit user press the search button
after he press search button pass searchText
}

Ability to Hit Enter on iPad

I want enter(Go.... etc) button on my iPad keypad. So that when I hit enter button on iPad keypad after entering username & password it should login & open next screen. I mean no need to press login button which I have design on my app. I have html pages in my application & I am able to hit Go,Search, refresh button by default. I want same Go button for iPad in Objective-C, Xcode framework. Only return button I can view by default on iPad, xcode framework . Can someone help me.....It can be easy one but I didnt found any code for the same.
It isn't hard at all.
Go into Interface Builder and open the current XIB you have placed the UITextField (I'm assuming).
What you want to do is, in the UITextField's property, is change the Return Key value to "Go" or whatever it is you want. I'm assuming you've already built the code for it to detect what UITextField is being edited and when the return key is tapped, what to do?
EDIT
Make sure that your UITextField's are delegated to the current ViewController. Then implement a delegate method. The following.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == emailInput) {
[emailInput resignFirstResponder];
[passwordInput becomeFirstResponder];
} if (textField == passwordInput) {
[passwordInput resignFirstResponder];
[self performSelector:#selector(userLogin)];
}
return 0;
}
Therefore, when the emailInput's return key is hit, we jump to the passwordInput. When the passwordInput's return key is then hit, we perform a selector that issues the user login.