How to get keyboard with Next, Previous and Done Button? - iphone

I want to have a keyboard which has a Next,Previous and Done button on top of it.
I have seen that in many apps.
Especially where there are forms to be filled.
I want to achieve something similar to above keyboard
How can I get that?

You'll find the answer on this other post.
I checked the iOS Library and the inputAccessoryView of a UITextField is exactly what you're looking for !
Hope this helps !

I just created a class called BSKeyboardControls which makes it very easy to add the controls to a keyboard. The class, instructions and example code can be found here at GitHub.
The controls works for text fields and text views and are optimized for both iPhone and iPad.

-(BOOL)textFieldShouldBeginEditing: (UITextField *)textField
{
UIToolbar * keyboardToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
keyboardToolBar.barStyle = UIBarStyleDefault;
[keyboardToolBar setItems: [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Previous" style:UIBarButtonItemStyleBordered target:self action:#selector(previousTextField)],
[[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(nextTextField)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(resignKeyboard)],
nil]];
textField.inputAccessoryView = keyboardToolBar;
}
- (void)nextTextField {
if (textField1) {
[textField1 resignFirstResponder];
[textField2 becomeFirstResponder];
}
}
-(void)previousTextField
{
if (textField2) {
[textField2 resignFirstResponder];
[textField1 becomeFirstResponder];
}
}
-(void)resignKeyboard {
[textField1 resignFirstResponder];
[textField2 resignFirstResponder];
}

I have a utility class that basically does this for you.
https://github.com/kalvish21/CustomKeyboard
The idea is very simple. You have to add an accessory tool bar with bar button items on it. There's a delegate which defines where what that button will do.

https://github.com/hackiftekhar/IQKeyboardManager
This is the best keyboard handler I have seen so far. Very excellent way to manage Text inputs.
Some of its features
1) ZERO LINE OF CODE
2) Works Automatically
3) No More UIScrollView
4) No More Subclasses
5) No More Manual Work
6) No More #imports

This is a custom control which is placed directly above the keyboard. I think a UIToolbar can be used for that.
Previous and next passes around the firstResponder of the textFields and Done will do the resign as well as hide the toolbar.
To match the keyboard animation have a look at this code I found or at SO: "What is the iPhone's default keyboard animation rate?"

I have created a repository with an implementation of this feature that works on iPhone/iPad in all orientations and highly customizable.

As mentioned in other answers, it's the inputAccessoryView that you're looking for.
I would like to add an alternative way here, by using this cocoapods:
pod 'UITextField-Navigation'
All UITextFields will have two additional properties: nextTextField and previousTextField. You can then simply connect the nextTextField in the Interface Builder and the Previous, Next and Done buttons are added automatically for you, all functional, no more code is needed, no subclassing.
You can also customize the UI, add more buttons, etc as you want to.

Related

UITextView next and previous Button over the Keyboard

how can I implement "Next" and "Previous" Button's over the keyboard.
I use UITextView and with click on "Next" the Next TextView gets focus, Previous the last TextView.
I do not found any tutorial on the Internet.
I hope everybody can help me.
Please use this custom control
COCOA
If you need any help, let me know.
This controlis very simple to use.
Updated Answer
Change the Button Label
Go to BSKeyboardControls.m file & make the following Changes
On line no. 42
[self setSegmentedControl:[[UISegmentedControl alloc] initWithItems:#[ NSLocalizedStringFromTable(#"<", #"BSKeyboardControls", #"Previous button title."),
NSLocalizedStringFromTable(#">", #"BSKeyboardControls", #"Next button title.") ]]];
On line No. 51
[self setDoneButton:[[UIBarButtonItem alloc] initWithTitle:NSLocalizedStringFromTable(#"Ok", #"BSKeyboardControls", #"Done button title.")
style:UIBarButtonItemStyleDone
target:self
action:#selector(doneButtonPressed:)]];
///// ********* Update 2 ********* ////////////
SET Image for Previous & Next
Just add the following code in BSKeyboardControls.m after line no. 52
[self.segmentedControl setImage:[UIImage imageNamed:#"add.png"] forSegmentAtIndex:BSKeyboardControlsDirectionPrevious];
[self.segmentedControl setImage:[UIImage imageNamed:#"add.png"] forSegmentAtIndex:BSKeyboardControlsDirectionNext];
change BSKeyboardControls.m file & make the following Changes
remove this one
// [self setRightArrowButton:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:106 target:self action:#selector(selectNextField)]];
add this
[self setRightArrowButton:[[UIBarButtonItem alloc]initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self action:#selector(selectNextField)]];

add custom switch to iphone view

I am using the following code to create a custom switch:
UISegmentedControl* switchView=[[UISegmentedControl alloc]
initWithItems:[[NSMutableArray alloc] initWithObjects:#"On",#"Off",nil]];
[switchView setFrame:CGRectMake(20,365,140,28)];
switchView.selectedSegmentIndex=0;
switchView.segmentedControlStyle=UISegmentedControlStyleBar;
[switchView setImage:[UIImage imageNamed:#"onSelected.png"] forSegmentAtIndex:0];
[switchView setImage:[UIImage imageNamed:#"off.png"] forSegmentAtIndex:1];
[switchView addTarget:self action:#selector(checkOnOffState:) forControlEvents:UIControlEventValueChanged];
I would now like to add this control to one of my screens (I believe the correct term is ViewController?). How would I do this in Xcode 4.3?
I looked at a few other posts and was not able to find anything that works. For example, How do I add a custom view to iPhone app's UI? suggested something along the lines of
[[myVC view] addSubview:myView];
and How to customize UISwitch button in iphone? suggested
self.navigationItem.titleView=switchView;
If possible, could you also explain why these approaches did not work? Thanks.
You need to create a class for your switch which is subclass of your UISegmentControl ,
Else you can add this code in your vc directly as well.
Add this code in its initWithFrame method
and then use it in your VC
-(void)viewDidLoad{
urCustomSwitch = [urCustomSwitch alloc] initWithFrame:CGRectMake(20,365,140,28)];
[self.view addSubview:urCustomSwitch];
}
This shall serve your purpose if you are to create custom reusable class.
If you add this switch control i.e. UISegmentedControl in InterfaceBuilder (.xib file)
This task will be much easier for you.

backBarButtonItem(leftbackButtonItem) Style Issue

I want change other UIBarButtonItem. but, retain buttonItemStyle...
I want the original buttonItemStyle... only change inner content
How make BackBarButtonItemStlye?
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"section" style:<??? what is style?> target:self action:#selector(popView:)];
how i get the image button style?
plz.. help ^^
I never find this style.
api only support below style... not backbuttonstyle...
UIBarButtonItemStylePlain,UIBarButtonItemStyleBordered,UIBarButtonItemStyleDone,
[self navigationItem] setBackBarButtonItem:backBarButtonItem]; is Not Want...
[self navigationItem] setLeftBarButtonItem:backBarButtonItem]; is Want..
because my custom buttonItem target, action is not nil...
my buttonItem is change custom item...
setBackBarButtonItem is only use default Action (now ViewController pop)
setLeftBarButtonItem is use any custom action
ex:
[ self navigationController] popToRootViewController];
[[self navigationController] popToViewController:[[[self navigationController] viewControllers] objectAtIndex:2] animated:YES];
big issue is not supported cocoa API backBarButtonItemStyle(image refrence)...
how to solve this problem?
only draw a illustrator backBarButtonItemStyle image?
You need to change the backBarButtonItem of the parent view controller. Use UIBarButtonItemStylePlain as a style and UIKit will automatically use the back appearance.
This style is not customizable. You need to create the bar button with some image that looks like the back button.

UISegmentedControl and registering callback without XIB

I have added a UISegmented control as follows (this seems to work just fine, but let me know if this is a bad practice):
mapType = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects: #"Map", #"Satelitte", #"Hybrid", nil]];
[_mapView addSubview:mapType];
Then I have a method that I would like to be called when this UISegmentedControl is changed, however, I cannot set the delegate or anything in the interface builder because I am doing this programmatically.
- (void)segmentedChangedType {
// some code here
}
How can I connect these? Thanks in advance for any help!
If the only solution is to use XIB, then I can do that, but would like to know how to do this if it's possible.
OK, figured it out, you add this:
[mapType addTarget:self action:#selector(changeType) forControlEvents:UIControlEventValueChanged];

How to toggle between UITextView's inputviews

I am getting problem in making my application compatible with iphone 4.0
My problem is like this.
When i click on a UITextview a custom keyboard is showing
UIView *test=[[UIView alloc] initWithFrame:CGRectMake(0, 160, 320, 215)];
UIExtendedKeyBoard *objKeyBoard =[[UIExtendedKeyBoard alloc] initWithFrame:[test bounds]];
objKeyBoard.tag =1001;
[test addSubview:objKeyBoard];
[objKeyBoard release];
[vTxtPoem setInputView:test];
[test release];
but when i click on particular button i need to toggle between default key board and my custom key board....i am using the following code to do this
[vTxtPoem setInputView:nil];
but it is not working ...even it is not showing default keyboard..it is still remain same custom keyboard. the control also coming to this line.
My first guess would be something like this:
[vTxtPoem resignFirstResponder];
[vTxtPoem setInputView:nil];
[vTxtPoem becomeFirstResponder];
A better way might be
[vTxtPoem setInputView:nil];
[vTxtPoem reloadInputViews];
See http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/UIResponder/reloadInputViews for details!