I can provide code if needed, however my problem looks fundamendal. I have a UITextField in a view that can copy and paste in it. After the action I cannot do it again. It works only once.
What might be the reason behind it? Is it possible that the paste menu is not shown because of another view in the window?
some code:
myTextField = [[UITextField alloc] initWithFrame:CGRectMake(0,1,320,50)];
[myTextField setFont:[UIFont boldSystemFontOfSize:40]];
[myTextField setTextColor:[UIColor whiteColor]];
[myTextField setText:#""];
[myTextField setBorderStyle:UITextBorderStyleNone];
[myTextField setEnabled:YES];
[myTextField setKeyboardType:UIKeyboardTypePhonePad];
[myTextField setDelegate:self];
myTextField.inputView = hiddenView;
and
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if(action == #selector(paste:))
return YES;
return NO;
}
Do I need to add something in the viewWillAppear method related with the UITextField? As I said the first time works fine.
UPDATE: After the first paste the copy/paste/select mechanism stopped working on my application in ALL views...
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (sel_isEqual(action, #selector(paste:)))
{
return YES;
}
return [super canPerformAction:action withSender:sender];
}
Related
Keyboard not resigning second time on the same textfield. I used UITextFieldDelegate. For Ex: I enter something in Name textField and I resign the keyboard. I clicked in MobileNo field entered something. This time keyboard not resigning.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Make delegate of your MobileNo
MobileNo.delegate=self;
Or if your are using xib then make Outlet connection of delegate
Try this,
- (void)viewDidLoad
{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
}
-(void)dismissKeyboard
{
[self.view endEditing:YES];
}
I've added my UITextField and UITextView inside a UIWindow I can not able to type inside in both of them. Both are not letting me allow to type in those fields. And, can't able to dismiss it when return pressed in keyboard.
-(void)showAlert
{
alertWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
alertView = [[UIView alloc] initWithFrame:CGRectMake(10, 115, 300, 230)];
alertWindow.windowLevel = UIWindowLevelAlert; // puts it above the status bar
alertView.center = CGPointMake(alertWindow.frame.size.width/2, alertWindow.frame.size.height/2);
alertView.backgroundColor = [UIColor whiteColor];
alertView.layer.borderWidth = 1.0;
alertView.layer.borderColor = [[UIColor whiteColor]CGColor];
UITextField *txt = ....
....
....
txt.delegate = self;
...
...
[alertView addSubview:txt];
UITextView *txtview = ...
....
....
txtview.delegate = self;
...
[alertView addSubview:txtview];
[alertWindow addSubview:alertView];
[alertWindow addSubviewWithZoomInAnimation:alertView duration:1.0 option:curveValues[0]];
[alertWindow setHidden:NO];
}
I've declared both of the delegate methods also. I placed a breakpoint there and checked also, even that method not yet called.
Update -
textFieldShouldReturn delegate method
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[reserveView resignFirstResponder];
[reserveWindow resignFirstResponder];
[self.reserveWindow endEditing:YES];
[reserveView endEditing:YES];
return YES;
}
You should try this for dismiss the keyboard from the UIWindow
[self.window endEditing:YES];
Source
I found, that method has not been called because of UIWindow So, i changed my UIWindow to UIView And, i fixed this issue by using UIView+Animation.h from here It provides the animation what i required exactly.
- (void) addSubviewWithZoomInAnimation:(UIView*)view duration:(float)secs option:(UIViewAnimationOptions)option;
- (void) removeWithZoomOutAnimation:(float)secs option:(UIViewAnimationOptions)option;
Thanks to Google! Cheers!
The keyboard will never dismiss on its own (even if you hit return!). You'll notice that in your delegate methods, you will receive events whenever you press return/done/etc.
A simple way to make the keyboard dismiss every time someone hits the return key is to implement the - (BOOL)textViewShouldEndEditing:(UITextView *)textView method.
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
[textView endEditing:YES];
return YES;
}
Edit: In your post you edited to say that you've declared both of the delegate methods... there are more than two so could you clarify what methods you've implemented? http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html#//apple_ref/occ/intf/UITextViewDelegate
Try this
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.window addGestureRecognizer:tapGesture];
//[self.view addGestureRecognizer:tapGesture];
[tapGesture release];
}
-(void)dismissKeyboard
{
[txt resignFirstResponder];
[textView resignFirstResponder];
}
Try this
That's the delegate method of textfield:-
(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[txtview resignFirstResponder];
return NO;
}
I have narrowed this down to only IOS6 devices.
Original Question:
I have searched everywhere and tried everything and I simply cannot figure this out!
I have a UITextField. When it is selected, the keyboard slides up and the curser begins blinking on the textfield. When I tap the buttons on the keyboard everything behaves normally, the key responds and the curser stops blinking as it should. The only thing is that text is not put into the textfield! It does however accept input from the emoji keyboard. Any help will be very much appreciated!
This is how the UITextField is created:
UITextField *userInput = [[UITextField alloc] initWithFrame:CGRectMake(10, yPos, controlWidth, 26)];
yPos += 32;
[userInput setText:[prompt defaultValue]];
[userInput setPlaceholder:[prompt helpText]];
[userInput setBorderStyle:UITextBorderStyleRoundedRect];
[userInput setClearButtonMode:UITextFieldViewModeWhileEditing];
[userInput setFont:[UIFont fontWithName:#"Helvetica" size:18]];
[userInput setAdjustsFontSizeToFitWidth:YES];
[userInput setMinimumFontSize:10];
[userInput setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[userInput setDelegate:self];
[self.scrollView addSubview:userInput];
[self.promptControls addObject:userInput];
[userInput release];
This is in a for loop so the number of UITextFields depends on how many times the loop is called.
These are the delegate methods I use:
- (void)textFieldDidBeginEditing:(UITextField *)aTextField
{
self.activeField = aTextField;
}
- (void)textFieldDidEndEditing:(UITextField *)aTextField
{
self.activeField = nil;
}
- (BOOL)textFieldShouldReturn:(UITextField *)aTextField
{
[aTextField resignFirstResponder];
return YES;
}
activeField is a property on the view controller just to determine which field is currently active
I believe that is all the relevant code
Ensure that you are setting up your window correctly at launch
And that you are calling
[window setRootViewController:someViewController];
Which is now an error in iOS6 if you dont and...
[window makeKeyAndVisible];
Which isn't an error (to omit) but throws things into wierd land and seems to result in UITextView and UITextField not working well.
as in...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.viewController = [[[MyViewController alloc] initWithNibName:nil bundle:nil] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:self.viewController] autorelease];
[window setRootViewController:navigationController];
[window makeKeyAndVisible];
}
I am not sure, but it might be a simple problem of text color. Try to set the text color;
[userInput setTextColor:[UIColor blackColor]];
I hope it helps.
Try Commenting [userInput setDelegate:self];
This may because the window textfield in not the key window.
Only the key window can accept the user input.
So, try make the [textfield.window makeKeyAndVisible];
I've searched through this site that I couldn't find a solution to the problem I'm facing now. Hope someone can help.
I've created a UIAlertView to prompt user to enter their name in an iPhone app.
UIAlertView *enterNameAlert = [[UIAlertView alloc] initWithTitle:#"Enter your name"
message:#"\n\n\n"
delegate:self
cancelButtonTitle:NSLocalizedString(#"Cancel", nil)
otherButtonTitles:NSLocalizedString(#"OK", nil),nil];
UITextField *enterNameField = [[UITextField alloc] initWithFrame:CGRectMake(16, 83, 252, 25)];
enterNameField.keyboardAppearance = UIKeyboardAppearanceAlert;
enterNameField.borderStyle = UITextBorderStyleRoundedRect;
enterNameField.autocorrectionType = UITextAutocorrectionTypeNo;
enterNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
enterNameField.returnKeyType = UIReturnKeyDone;
enterNameField.delegate = self;
[enterNameField becomeFirstResponder];
[enterNameAlert addSubview:enterNameField];
[enterNameAlert show];
[enterNameAlert release];
[enterNameField release];
I've setup this viewController to comply with UITextFieldDelegate in the header file, and implemented textFieldShouldReturn: trying to dismiss the keyboard when user hit Done.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
NSLog(#"text field was first responder");
} else {
[textField becomeFirstResponder];
[textField resignFirstResponder];
NSLog(#"text field was not first responder");
}
NSLog(#"textfieldshouldreturn");
return YES;
}
In the debugger, I confirm that this method is called successfully when I tap on the Done button, with the textField being the first responder at that time. However, the keyboard doesn't go away, but the little clearButton goes away. It is clear that the textField is no longer the first responder because when I click the Done button again, nothing get called. I just want to dismiss the keyboard with the Done button. Can anyone please offer a solution? Million thanks.
First you need to define your enterNameAlert in interface header. then use the below code.
- (void)viewDidLoad {
[super viewDidLoad];
enterNameAlert = [[UIAlertView alloc] initWithTitle:#"Enter your name"
message:#"\n\n\n"
delegate:self
cancelButtonTitle:NSLocalizedString(#"Cancel", nil)
otherButtonTitles:NSLocalizedString(#"OK", nil),nil];
UITextField *enterNameField = [[UITextField alloc] initWithFrame:CGRectMake(16, 83, 252, 25)];
enterNameField.keyboardAppearance = UIKeyboardAppearanceAlert;
enterNameField.borderStyle = UITextBorderStyleRoundedRect;
enterNameField.autocorrectionType = UITextAutocorrectionTypeNo;
enterNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
enterNameField.returnKeyType = UIReturnKeyDone;
enterNameField.delegate = self;
[enterNameAlert addSubview:enterNameField];
[enterNameField becomeFirstResponder];
[enterNameAlert show];
[enterNameField release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
[enterNameAlert dismissWithClickedButtonIndex:1 animated:YES];
[enterNameAlert release];
NSLog(#"text field was first responder");
} else {
[textField becomeFirstResponder];
[textField resignFirstResponder];
NSLog(#"text field was not first responder");
}
NSLog(#"textfieldshouldreturn");
return YES;
}
What happens if you move the releasing of the text field to the delegate method, after you call the resignFirstResponder method?
Try dismissing keyboard using resignFirstResponder on "DidEndOnExit" event of your TextField.
Then try to dismiss keyboard by pressing Done button on keyboard when you have finished entering the data into your TextField.
Hope this helps.
Have another object become and then immediately resign the first responder.
simply do
[mytextField addTarget:self
action:#selector(methodToFire:)
forControlEvents:UIControlEventEditingDidEndOnExit];
if anyone is having trouble with this in storyboard i had to go into my uitextfields properties and ctrl+drag from its delegate to its root view controller all in storyboard and that fixed everything..
I have two text fields in my view. I did it using IB. My problems are
After entering the text in textField1 I am entering text in textField2. When I click in textField1 again the previous text is disappeared in the textField1.
After entering the text in both the textFields, I need the keyboard to disappear. But, even I touched the return key in the keyboard layout or I touched the screen outside the text field the keyboard is not disappearing.
How can I make this.
Thank you.
- (void)viewDidLoad
{
self.title = #"Edit";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStyleDone target:self action:#selector(save)];
//[nameField becomeFirstResponder];
nameField.clearsOnBeginEditing = NO; //First text field
[nameField resignFirstResponder];
//[descriptionField becomeFirstResponder];
descriptionField.clearsOnBeginEditing = NO; //second text dield
[descriptionField resignFirstResponder];
[super viewDidLoad];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == nameField) {
[nameField resignFirstResponder];
}
else if(textField == descriptionField){
[descriptionField resignFirstResponder];
}
return YES;
}
I did in the above way but the keyboard is still not disappearing. And after entering the text in textField1 when I press return key the cursor is not going to textField2. How can I make it work ?
Thank You.
For problem 1, check the clearsOnBeginEditing property of the text field.
For problem 2, you can send the resignFirstResponder message to the text field.
See Managing Keyboard section in the UITextField documentation.
I got the result. First thing in my mistake is I did not add UITextFieldDelegate in interface. And the remaining I did the changes in code.
- (void)viewDidLoad {
self.title = #"Edit";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStyleDone target:self action:#selector(save)];
[nameField becomeFirstResponder];
nameField.delegate = self;
descriptionField.delegate = self;
[super viewDidLoad];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(#"Return Key Pressed");
if (textField == nameField) {
[nameField resignFirstResponder];
[descriptionField becomeFirstResponder];
}
else if(textField == descriptionField){
[descriptionField resignFirstResponder];
}
return YES;
}
After entering the text in textField1 if I touch return the cursor goes to textFeild2. It is working good. But when I touch the screen out of text field the keyboard is not disappearing.
Thank you.