Am trying to show UIKeyboard without using UITextView and UITextField. Am following the below source code to show the Keyboard in my app.
UIKeyboard *keyboard = [[[UIKeyboard alloc] initWithFrame: CGRectMake(0.0f, 220.0f, 320.0f, 216.0f)] autorelease];
[keyboard setReturnKeyEnabled:NO];
[keyboard setTapDelegate:editingTextView];
but the code showing below errors,
Use of undeclared identifier 'UIKeyboard'
Use of undeclared identifier 'keyboard'
Anyone please help to solve this error? and please tell me this is the right way to show keyboard without using UITextView and UITextField?
You can use UIKeyInput delegate.
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UIKeyInput>
And in your ViewController.m class. return YES; to canBecomeFirstResponder:
-(BOOL)canBecomeFirstResponder
{
return YES;
}
It will show you keyboard. But Where you write if there is no UITextView or UITextField.
Well for writing on UIView see this logics - UIKeyInput- Displaying keyboard in iPhone without UITextField or UITextView
Related
I have a simple question, how to connect a textfield to another Control view, if I click on the textfield, instead of show the keyboard it jump to another view
Thanks for answers
In the delegate method of the TextField, wich is textFieldShouldBeginEditing, add code that go from current View to another View.
Or you can use tap gasture recognizer on TextField to get the touch.
Here is the code:
create a TextFied in your IB and connect it to .h file
#interface ViewController : UIViewController<UITextFieldDelegate>
#property (strong, nonatomic) IBOutlet UITextField *firstTF;
And in .m file add this
#synthesize firstTF;
- (void)viewDidLoad
{
[super viewDidLoad];
firstTF.delegate= self;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
secondViewController *ainfoController = [[secondViewController alloc] initWithNibName:#"secondViewController" bundle:nil];
[self presentModalViewController:ainfoController animated:YES];
return YES;
}
If you are adding the Textfield by code then,
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(x, y, width, height)];
textField.delegate = self;
[self.view addSubview:textField];
and add this method
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField{
// you can add the code for present a new viewController here
return NO;
}
make sure you have added UITextFieldDelegate in your .h file
I really don't get in why you need this but as a developer we do firmly believe to implement all scenarios, so in your case, you can achieve the same by following the any one beneath mentioned tacts:
1) In this approach, you need to override the textfieldshouldbegin delegate and use the navigate code for moving from one screen to another and don't forget to call resignFirstResponder here.
2) While in this second approach what you can do, just overlap a custom button(with neither image nor any text) and just on his click event method write your navigation code to move another screen.
Do that stuff, you'll get what you want and in case still you find any difficulty just shout over me.
Does anyone know why im getting runtime error when i click on return button on iphone keyboard. I need to hide keyboard after done editing values to UITextField. So i assigned Did End On Exit to IBAction and the IBAction code below
-(IBAction)FinishEditing:(id)sender
{
[folderName resignFirstResponder];
}
When running ma project i facing a runtime error and the variable values shown below
argv char ** 0xbffff58c
*argv char * 0xbffff6b8
**argv char '/'
Console Value
(lldb)
Any idea to overcome this issue??
According to your question you want to hide keyboard on return button click of keyboard. So there is no need to make any button action for this.. you can do that by UITextField delegate method. Add UITextFieldDelegate in ViewController.h file and then simply write below method in ViewController.m file :-
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
It would return on return button click of keyboard.
You can use the textfield delegate method to do the process .No need to pin the IBActions for this one use
– textFieldShouldBeginEditing:
– textFieldDidBeginEditing:
– textFieldShouldEndEditing:
– textFieldDidEndEditing:
refer this
and
This is a nice tutorial
Thanks guys for helping me. Finally i figure out my issue..
initially my code is like this
AddFolder *addButton = [[AddFolder alloc] initWithNibName:#"AddFolder" bundle:[NSBundle mainBundle]];
[self.view addSubview:addButton.view];
[addButton release];
And now ma code is like this
AddFolder *addButton = [[AddFolder alloc] initWithNibName:#"AddFolder" bundle:[NSBundle mainBundle]];
[self.view addSubview:addButton.view];
We dont need to release memory after adding a subview.
hi i'm studying iOS programming.
i have a trouble to use UITextView.
i made a project and open viewController.xib
i dragged in UISearchBar, UITextView and UILabel
and my viewController is followed UISearchBarDelegate
i want to change textView.text's contents but it doesn't.
here's my code.
-(void)viewDidLoad
{
[super viewDidLoad];
self.mySearchbar.delegate = self;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
self.myLabel.text = #"hoho";
self.myTextview.text = #"hoho";
}
when i clicked search bar and typing any character and return,
myLabel's text is set to hoho.
but myTextview.text doesn't set. no effect.
why is that?? i'm confused.. please help me
Did you link up the Outlet in Interface Builder? In IB, click File's Owner, select Outlets on the right pane, link myTextview to the UITextview you put on the view?
Also, since you are calling "self" I assume you created a property for myTextview as an IBOutlet in your .h file?
Are you setting(connecting) the delegate of UITextView as well. That might be the problem.
Try to set the TextView Color to black
[YourTextview setTextColor:[UIColor blackColor]];
Sometimes this helps.
As I recall, instead you should be using
[self.myLabel setText:#"hoho"];
I'm trying to hide the number pad, but I do not want to implement a button.
Is there a way to dismiss the number pad when the user taps outside the textfield?
This is one of those questions where you read it and say "That's easy you just..". And then you go to do it and make it super complicated. And then realize it doesn't have to be that complicated.
The answer I've come up with, and I'm sure it will help someone else, Is to use an invisible UIView that never interacts but acts on other views and maybe not in the way you'd think.
The typical answer to a question about dismissing the UIKeyboardTypeNumberPad keyboard is to add a bar that has a button as the inputAccessoryView to dismiss the keyboard. If a bar and button are undesirable generally you just listen for touch events on the background and your good to go but this question is about a tableview and that makes this much harder.
But this inputAccessoryView feature is still awesome. It allows you to define a UIView or UIView subclass to be displayed when the keyboard is shown. More importantly when the keyboard is shown due to a textfield for which it is the inputAccessoryView becoming first responder.
I could yammer on but first here is some code for a lightweight class that actually performs very well in testing.
The contents of NJ_KeyboardDismisser.h are:
#import <UIKit/UIKit.h>
// For some reason neither inputView or inputAccessoryView are IBOutlets, so we cheat.
#interface UITextField (WhyDoIHaveToDoThisApple)
#property (readwrite, retain) IBOutlet UIView *inputAccessoryView;
#end
#interface NJ_KeyboardDismisser : UIView
#property (nonatomic, weak) IBOutlet UIView *mainView;
-(id)initWithMainView:(UIView *)view; // convienience method for code
#end
And the contents of NJ_KeyboardDismisser.m are:
#import "NJ_KeyboardDismisser.h"
#implementation NJ_KeyboardDismisser {
UITapGestureRecognizer *_tapGR;
}
#synthesize mainView = _mainView;
-(void)setMainView:(UIView *)view{
if (_tapGR) [_tapGR.view removeGestureRecognizer:_tapGR];
_mainView = view;
_tapGR = [[UITapGestureRecognizer alloc] initWithTarget:_mainView action:#selector(endEditing:)];
}
-(id)initWithMainView:(UIView *)view{
if ((self = [super initWithFrame:CGRectMake(0, 0, 0, 0)])){
self.mainView = view;
}
return self;
}
-(void)didMoveToWindow{ // When the accessory view presents this delegate method will be called
[super didMoveToWindow];
if (self.window){ // If there is a window one of the textfields, for which this view is inputAccessoryView, is first responder.
[self.mainView addGestureRecognizer:_tapGR];
}
else { // If there is no window the textfield is no longer first responder
[self.mainView removeGestureRecognizer:_tapGR];
}
}
#end
You may recognize the endEditing: method, as mentioned by Cosique, it is a UIView extension method that asks a views nested textfield to resign. Sound handy? It is. By calling it on the tableview the textfield it contains resigns first responder. Since this technique works on all UIViews there is no need to artificially limit this outlet to only UITableViews so the outlet is just UIView *mainView.
The final moving part here is the UITapGestureRecognizer. We don't want to add this recognizer full time for fear of screwing up the tableview's workings. So we take advantage of UIView's delegate method didMoveToWindow. We don't really do anything with the window we just check to see if we are in one; If we are then one of our textfields is first responder, if not then it's not. We add and remove our gesture recognizer accordingly.
Okay straightforward enough, but how do you use it? Well if instantiating in code you could do it like this, in tableView:cellForRowAtIndexPath::
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(20, 6, 100, 31)];
[cell.contentView addSubview:field];
field.keyboardType = UIKeyboardTypeNumberPad;
field.inputAccessoryView = [[NJ_KeyboardDismisser alloc] initWithMainView:self.view];
}
If you are using static cells in a storyboard then the technique is different (obviously). First drag out a generic NSObject and place it in the dark grey strip below the view (where the other objects such as the view controller are). Then change this new object's class to be NJ_KeyboardDismisser. Then connect the "Keyboard Dismisser"'s mainView property to that view (generally a tableview). Then connect the inputAccessoryView property from any each text field in that scene you wish to the "Keyboard Dismisser".
Give it a try! The tableview acts normally. Apple's tap recognizer is smart enough to ignore the swipes on the table, so you can scroll. It also ignores touches in the textfields so you can edit and select other textfields. But tap outside a textfield and the keyboard is gone.
Note: This class's use is not limited to tableviews. If you want to use it on a regular view, just set the mainView property to be the same as the view controller's view.
The easiest way is to do this in your view controller:
[self.view endEditing: YES];
You can resign the responder inside the below function for your view:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
Make sure your view is enabled for user interaction.
when creating the text field add a tag to it.
like this Yourtextfield.tag = 1;
and in you touchesEnded method
do this :
UITextField *resignTextField = (UITextField *)[self.view viewWithTag:1];
[resignTextField resignFirstResponder];
When my UITextView is being edited, I want a button to appear. How can I do this?
Thanks!
Implemetn UITextViewDelegate in your class and when editing is being started on UITextView
it'll call it's textViewShouldBeginEditing: function where you can do whatever you want. You can show your button when editing start and hides when editing ends in this function.
– textViewShouldEndEditing
For further detail see this
i will detail above answer a little bit:
YourViewController.h
#interface YourViewController : UIViewController <UITextViewDelegate>
YourViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.yourTextView.delegate = self;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
// do smth like create a button
return TRUE;
}
Read Apple manual for more
Check out the UITextViewDelegate Protocol Reference. If you set your UIViewController to be the delegate of the UITextView, it will be sent textViewDidBeginEditing: and textViewDidEndEditing:. You can show/hide your button when you receive those messages.
//in view did load.
UITextField entered1 = [[UITextField alloc] initWithFrame:CGRectMake(120.0, 85.0, 150.0, 25.0)];
[entered1 setBackgroundColor:[UIColor whiteColor]];
[entered1 addTarget:self action:#selector(example:)forControlEvents:UIControlEventTouchDown];
[self.view addSubview:entered1];
[entered1 release];
or
[textfieldname addTarget:self action:#selector(example:)forControlEvents:UIControlEventTouchDown];
.........
}
-(IBAction) example:(UIControl *)sender
{
//your code here.
}
Xcode 6.4, iOS 8.4, ARC Enabled
Don't make my mistake, took me a long time to figure it out, but I was adding...
#interface YourViewController : UIViewController <UITextFieldDelegate>
because I am used to working with UITextField's, but obviously we need...
#interface YourViewController : UIViewController <UITextViewDelegate>
Hope this saves someone time. Cheers!