Hi I have a big problem with iPhone 4.0 OS with this code
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES) {
}
in condition UIKeyboard it not working. I try "UILayoutContainerView"
but it not working too.
please.
You can try this code:
- (BOOL) findKeyboard:(UIView *) superView;
{
UIView *currentView;
if ([superView.subviews count] > 0) {
for(int i = 0; i < [superView.subviews count]; i++)
{
currentView = [superView.subviews objectAtIndex:i];
NSLog(#"%#",[currentView description]);
if([[currentView description] hasPrefix:#"<UIKeyboard"] == YES)
{
NSLog(#"Find it");
return YES;
}
if ([self findKeyboard:currentView]) return YES;
}
}
return NO;
}
-(void) checkKeyBoard {
UIWindow* tempWindow;
for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; c ++)
{
tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c];
if ([self findKeyboard:tempWindow])
NSLog(#"Finally, I found it");
}
}
- (void)keyboardWillShow:(NSNotification *)note {
[self performSelector:(#selector(checkKeyBoard)) withObject:nil afterDelay:0];
}
And you also need to add this code to function didFinishLaunchingWithOptions in AppDelegate:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
#"<UIPeripheralHostView" will work instead of #"<UIKeyboard"
Dunno why.
Can you tell me then: How to determine the keyboard type in the UIKeyboard class?
Related
I have tried the following code from another application but it doesn't find anything. Why? How to get access to the UIWebView toolbar?
- (UIToolbar *)findVirginWebKeyboardToolbar:(UIView *)parent
{
if ([parent isKindOfClass:[UIToolbar class]]) {
UIToolbar *tb = (UIToolbar *)parent;
if ([tb.items count] == 1 && [((UIBarButtonItem *)[tb.items objectAtIndex:0]).customView isKindOfClass:[UISegmentedControl class]]) {
return tb;
}
}
for (UIView *view in parent.subviews) {
UIToolbar *tb = [self findVirginWebKeyboardToolbar:view];
if (tb) return tb;
}
return nil;
}
- (void)removeKeyboardBar {
UIView *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
keyboardWindow = testWindow;
UIToolbar *toolbar = [self findVirginWebKeyboardToolbar:keyboardWindow/*subviewWhichIsPossibleFormView*/];
if (toolbar) {
itemsArray = [NSArray arrayWithObjects:button1, button2, button3, nil];
[toolbar setItems:itemsArray];
}
}
}
With the following code you should be able to remove the toolbar that is above the keyboard.
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification *)note {
[self performSelector:#selector(removeBar) withObject:nil afterDelay:0];
}
- (void)removeBar {
// Locate non-UIWindow.
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if (![[testWindow class] isEqual:[UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
// Locate UIWebFormView.
for (UIView *formView in [keyboardWindow subviews]) {
// iOS 5 sticks the UIWebFormView inside a UIPeripheralHostView.
if ([[formView description] rangeOfString:#"UIPeripheralHostView"].location != NSNotFound) {
for (UIView *subView in [formView subviews]) {
if ([[subView description] rangeOfString:#"UIWebFormAccessory"].location != NSNotFound) {
// remove the input accessory view
[subView removeFromSuperview];
}
else if([[subView description] rangeOfString:#"UIImageView"].location != NSNotFound){
// remove the line above the input accessory view (changing the frame)
[subView setFrame:CGRectZero];
}
}
}
}
}
This is my code....
`
- ( void ) registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- ( void ) keyboardWasShown:(NSNotification*)notification {
[self addButtonToKeyboard];
}
#pragma -
#pragma Numeric keyboard done button
- ( void ) keyboardDoneClicked:(id)sender {
[txtvannum resignFirstResponder];
[txtmobnum resignFirstResponder];
// [sender resignFirstResponder];
}
- ( void ) addButtonToKeyboard {
// create custom button
// if(keyButton){
doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
[doneButton setImage:[UIImage imageNamed:#"DoneUp3.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown3.png"] forState:UIControlStateHighlighted];
} else {
[doneButton setImage:[UIImage imageNamed:#"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown.png"] forState:UIControlStateHighlighted];
}
[doneButton addTarget:self action:#selector(keyboardDoneClicked:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES)
[keyboard addSubview:doneButton];
} else {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
// }
//// else{
//// return;}
}
`
I developed an app which consist of user to enter their details.There are 4 text fields in that.Two among those requires numeric type of keyboard.For that numeric keyboard I added the done button to resign after finished editing.But that done button is coming for all other type of keyboard also.How can add that done button oly to numeric keyboard so that it should hide for all other type.I am struggling in this.Please help.
Thank you.
For each UITextField use the function setReturnKeyType: which can attain values,
typedef enum {
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
} UIReturnKeyType;
And you have to set the key type for each text field separately.For the text field that you don't want the done button just set it as UIReturnKeyDefault.
Hope this helps.
First of all, you have to set a notification to keep tracking keyboard's activity. To identify, which UITextField is being active, you have to set an unique tag to each.
In .h file >>
#interface myViewController : UIViewController
{
UITextField *txtField1; // Allows Numeric input
UITextField *txtField2; // Allows String input
UITextField *txtField3; // Allows Numeric input
UITextField *txtField4; // Allows String input
UITextField *txtFieldTemp; // Temp textfield
BOOL isReturned;
}
In .m file >>
- (void)viewDidLoad
{
txtField1.tag = 0;
txtField2.tag = 1;
txtField3.tag = 2;
txtField4.tag = 3;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(UIKeyboardWillChangeFrameNotification:) name:UIKeyboardDidShowNotification object:nil];
}
- (void)UIKeyboardWillChangeFrameNotification:(NSNotification *)note
{
if (txtFieldTemp.tag == 0 || txtFieldTemp.tag == 2)
{
[self addButtonToKeyboard];
}
}
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
if (textField.tag == 0 || textField.tag == 2)
{
[self addButtonToKeyboard];
}
else
{
[self removeButtonFromKeyboard];
}
txtFieldTemp = textField;
}
- (void)removeButtonFromKeyboard
{
// Remove Keyboard logic
}
I have a simple app which consists of 2 views. Top view has a accelerometer delegates. when user shakes while top view is on the screen. then pushviewcontroller is called and subview will appear. the problem is when subview is appeard, i shake it, it still catches the shake action and results me an error. So help me. thanks in advance.
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
const float violence = 1.2;
static BOOL beenhere;
BOOL shake = FALSE;
if (beenhere) return;
beenhere = TRUE;
if (acceleration.x > violence || acceleration.x < (-1* violence))
shake = TRUE;
if (acceleration.y > violence || acceleration.y < (-1* violence))
shake = TRUE;
if (acceleration.z > violence || acceleration.z < (-1* violence))
shake = TRUE;
if (shake) {
[self playSound:#"suzu"];
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"noVib"] == NO) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
[[UIAccelerometer sharedAccelerometer] setDelegate:nil];
[self presentModalViewController:mMoviePlayer animated:YES];
[self play];
}
when play method called and video has finished, pushviewcontroller will be called and the sub view will appear
below is a class that handles all the movieplayer stuff.
- (void) initPlayer{
if (mMoviePlayer != nil){
[mMoviePlayer release];
}
mMoviePlayer = [[MoviePlayerViewController alloc] initWithContentURL:[self createURL]];
[[NSNotificationCenter defaultCenter] removeObserver:mMoviePlayer
name:MPMoviePlayerPlaybackDidFinishNotification object:mMoviePlayer.moviePlayer];
[mMoviePlayer.moviePlayer setShouldAutoplay:NO];
mMoviePlayer.moviePlayer.backgroundView.backgroundColor = [UIColor blackColor];
mMoviePlayer.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
mMoviePlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mMoviePlayer.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePreloadDidFinish:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:mMoviePlayer.moviePlayer];
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:mMoviePlayer.moviePlayer];
[self dismissModalViewControllerAnimated:YES];
[mMoviePlayer release];
mMoviePlayer = nil;
[self toNext];
}
The first thing that comes into my mind is to keep track if the mMoviePlayer is pushed or not.
if (shake && mPlayerPushed) {
[self playSound:#"suzu"];
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"noVib"] == NO) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
[[UIAccelerometer sharedAccelerometer] setDelegate:nil];
[self presentModalViewController:mMoviePlayer animated:YES];
[self play];
mPlayerPushed = YES;
}
in the moviePlayBackDidFinish method after you call dismissModalViewControllerAnimated:YES you should set the mPlayerPushed to NO.
or you could use the property modal view controller to see if a modal view is displayed. This i haven't tested yet but it should work.
modalViewController: The controller for the active modal view—that is,
the view that is temporarily displayed on top of the view managed by
the receiver. (read-only)
#property(nonatomic, readonly) UIViewController *modalViewController
so instead of the bool mPlayerPushed you will have:
if(shake && something.modalviewController == nil){ present mMoviePlayer }
I used to use the following code to add a UIToolbar just above the UIKeyboard and being attached to it. I just switched to the iPhone OS 4 and I realized that it's not working anymore.
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
for (UIView *keyboard in [keyboardWindow subviews]) {
//print all uiview descriptions
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES) {
.. Some code to attach the UIToolbar
}
}
}
I realized that the code does not get into the if statement. I tried printing out all UIView descriptions just above the if and I could not see anything related to UIKeyboard. The code was working fine in OS 3.0 and 3.1. So does anyone have any idea about it?
You should try to use this below code:
- (BOOL) findKeyboard:(UIView *) superView;
{
UIView *currentView;
if ([superView.subviews count] > 0) {
for(int i = 0; i < [superView.subviews count]; i++)
{
currentView = [superView.subviews objectAtIndex:i];
NSLog(#"%#",[currentView description]);
if([[currentView description] hasPrefix:#"<UIKeyboard"] == YES)
{
NSLog(#"Find it");
//add toolbar here
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, -40, 100, 40)];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Test button" style:UIBarButtonItemStyleBordered target:self action:#selector(buttonClicked:)];
NSArray *items = [[NSArray alloc] initWithObjects:barButtonItem, nil];
[toolbar setItems:items];
[items release];
[currentView addSubview:toolbar];
return YES;
}
if ([self findKeyboard:currentView]) return YES;
}
}
return NO;
}
-(void) checkKeyBoard {
UIWindow* tempWindow;
for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; c ++)
{
tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c];
if ([self findKeyboard:tempWindow])
NSLog(#"Finally, I found it");
}
}
- (void)keyboardWillShow:(NSNotification *)note {
[self performSelector:(#selector(checkKeyBoard)) withObject:nil afterDelay:0];
}
And you also need to add this code to function didFinishLaunchingWithOptions in AppDelegate:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
Since you are relying on undocumented behavior, it should be no surprise that it does not work in 4.0 anymore. Use your views' inputAccessoryView property instead, which is the documented way to do this in OS 3.2+.
How I can disappear numeric keyboard in iPhone because there is no return key in it, which implies it is not going to respond to
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Use resignFirstResponder in touchesBegan, like so:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if(touch.phase == UITouchPhaseBegan) {
[self.myTextField resignFirstResponder];
}
}
You may need to call this on multiple text fields if you're not sure where the focus is currently located; I haven't tested that case.
Additionally, in Interface Builder, you'll need to enable the "User Interaction Enabled" checkbox for the view, or programatically use:
myView.userInteractionEnabled = YES;
Here's the answer..
-(void) touchesBegan :(NSSet *) touches withEvent:(UIEvent *)event
{
//[URL resignFirstResponder];
//[Password resignFirstResponder];
[speedField resignFirstResponder];
[super touchesBegan:touches withEvent:event ];
}
Try this:
[theTextField resignFirstResponder];
Why not to use "Numbers and Punctuation" keyboard instead of "Numeric"? It will save your time
Here you go. Extend your UIViewController Class with this. But how do we know it is the done button? Adding Done Button to Only Number Pad Keyboard on iPhone
//
// UIViewController+NumPadReturn.m
// iGenerateRandomNumbers
//
// Created by on 12/4/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "UIViewController+NumPadReturn.h"
#implementation UIViewController (NumPadReturn)
-(void) viewDidLoad{
// add observer for the respective notifications (depending on the os version)
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
} else {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
}
}
- (void)keyboardWillShow:(NSNotification *)note {
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2) {
[self addButtonToKeyboard];
}
}
- (void)keyboardDidShow:(NSNotification *)note {
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
[self addButtonToKeyboard];
}
}
- (void)addButtonToKeyboard {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
[doneButton setImage:[UIImage imageNamed:#"DoneUp3.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown3.png"] forState:UIControlStateHighlighted];
} else {
[doneButton setImage:[UIImage imageNamed:#"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown.png"] forState:UIControlStateHighlighted];
}
[doneButton addTarget:self action:#selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES)
[keyboard addSubview:doneButton];
} else {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
}
- (void)doneButton:(id)sender {
NSLog(#"doneButton");
[self.view endEditing:TRUE];
}