UITextField will not accept input from the keyboard for IOS6 - iphone

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];

Related

Dissmissing the keyboard when UITextField and UITextview displayed inside of UIWindow

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;
}

UITextField missing the paste functionality

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];
}

UITextView Not Become Responding From Another UITextView Accessory View iPhone?

I am working on a message based iPhone app. I have a screen to reply to a received message. This screen contains two UITextViews like bottomTextView and topTextView.
topTextView is added as bottomTextView's InputAccessory view.
When the user enter into the screen topTextView has to becomeFirstResponder. It is showing but the cursor is not placed in the topTextView. The cursor is located in the textview bottomTextView. How to make topTextView become first responder with cursor in place?
Here is the code i have tried:
- (void)viewDidLoad
{
[super viewDidLoad];
bottomBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 380, 320, 40)];
bottomBarViewImage.image = [UIImage imageNamed: #"toolbarbg~iphone.png"];
[bottomBarView addSubview: bottomBarViewImage];
[self.view addSubview: bottomBarView];
bottomTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 7.5, 210, 25)];
bottomTextView.delegate = self;
bottomTextView.backgroundColor = [UIColor clearColor];
bottomTextView.font = [UIFont fontWithName:#"Helvetica" size:14];
bottomTextView.scrollEnabled = NO;
[bottomBarView addSubview: bottomTextView];
topBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 380, 320, 40)];
topBarViewImage.image = [UIImage imageNamed: #"toolbarbg~iphone.png"];
[topBarView addSubview: topBarViewImage];
topTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 7.5, 210, 25)];
topTextView.delegate = self;
topTextView.backgroundColor = [UIColor clearColor];
topTextView.font = [UIFont fontWithName:#"Helvetica" size:14];
topTextView.scrollEnabled = NO;
[topBarView addSubview: topTextView];
[bottomTextView becomeFirstResponder];
[bottomTextView setInputAccessoryView: topBarView];
}
-(void) textViewDidBeginEditing:(UITextView *)textView
{
if(textView == bottomTextView)
{
bottomTextView.scrollEnabled = NO;
[topTextView becomeFirstResponder];
}
}
The topTextView with topBarView is showing but the cursor is not placed in topTextView. Could you please help me to solve this issue? Thanks in advance.
I think it may be because you call [topTextView becomeFirstResponder]; in UITextView's delegate textViewDidBeginEditing:. So the topTextView only becomes first responder when you start editing bottomTextView. Try calling [topTextView becomeFirstResponder]; instead of [bottomTextView becomeFirstResponder]; in viewDidLoad. See how it goes. I'm not sure, but becomeFirstResponder may not call textViewDidBeginEditing:. Not sure it'll work, but worth the try...
EDIT :
I found a related problem here. It may be because the textView does not appear right away, so it can not become first responder. Here is the accepted answer, by #Tom :
My solution: check when the keyboard (and thus the accessory view)
appeared!
Step 1) Listen for the notification (make sure this code is read
before you want to receive the notification).
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(changeFirstResponder)
name:UIKeyboardDidShowNotification
object:nil];
Step 2) When the keyboard has appeared, you can set the textfield in
your inputaccessoryview to become first responder:
-(void)changeFirstResponder
{
[textField becomeFirstResponder]; //will return TRUE;
}

Why aren't UITabBarItems' titles displaying?

First time poster. Thanks in advance.
I want to use a UITabBar in my iPhone app. I have been led to believe that I cannot use UITabViewController because I would be nesting it inside a UINavigationController. I read that you can do this, but you have to use UITabBar instead of the full-fledged controller. I can make this work with Interface Builder, but I've got a lot of redundant XIBs and I'd rather do it in code anyway. I want to know how the magic works, as it were.
Here's the .m of my controller:
#import "DumbViewController.h"
#implementation DumbViewController
-(void) loadView {
UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 150, 30)];
[label setText:#"hello world"];
[view addSubview:label];
[label release];
UITabBar *tb = [[UITabBar alloc] initWithFrame:CGRectMake(0, [view frame].size.height - 64, [view frame].size.width, 64)];
[tb setDelegate:self];
[tb setItems:[NSArray arrayWithObject:[[[UITabBarItem alloc] initWithTitle:#"bob" image:[UIImage imageNamed:#"21-skull.png"] tag:0] autorelease]]];
[view addSubview:tb];
[tb release];
[self setView:view];
[view release];
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(#"selected item %#", item);
}
#end
When I run the app, I get my label and a tab bar at the bottom. I get the goofy little skull icon, but "bob" never displays. Clicking the skull works as you would expect.
Why isn't that title showing?
Thanks!
Try the following code....
UITabBarItem *someTabBarItem = [[UITabBarItem alloc] initWithTitle:#"bob" image:[UIImage imageNamed:#"21-skull.png"] tag:0];
NSArray *tabBarItems = [[NSArray alloc] initWithObjects:someTabBarItem,nil];
[tabBar setItems:tabBarItems animated:NO];
[tabBar setSelectedItem:someTabBarItem];
[tabBarItems release];
[someTabBarItem release];
The problem was that I was setting the frame of the UITabBar outside the frame of the parent view.
I added this line after the UITabBar alloc line.
[tb setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
The advantage here is that even if I rotate or if the app is running on an iPad, the tab bar stays at the bottom of the screen.
I also changed from a 64 pixel height to a 44 pixel height because it looks much more like how the XIB did originally.

ResignFirstResponder doesn't dismiss the keyboard (iPhone)

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..