如何在iPhone中使用键盘获取文本字段sdk

问题描述:

我正在gpsnote上做一个项目。如果任何人有任何信息生根粉这个应用程序PLZ指导我...如何在iPhone中使用键盘获取文本字段sdk

我知道这个代码BT,我想应该文本框和我的键盘来一起

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    [textField resignFirstResponder]; 
} 

感谢

+0

yoou可以使用becomeFirstResponder键盘沿文本域。 – rishi 2012-04-21 07:01:37

首先设置你的内容在滚动视图,设置在viewDidLoad中滚动视图:

[scrollView setContentSize : CGSizeMake (320, 1040)]; 

然后在viewWillAppear中添加以下通知:

对于键盘显示

​​

对于键盘隐藏

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillBeHidden:) 
              name:UIKeyboardWillHideNotification object:nil]; 

以下是这两个功能,通过通知称为

- (void)keyboardWasShown:(NSNotification*)aNotification { 

NSDictionary* info = [aNotification userInfo]; 
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

double keyboardHeight = kbSize.height; 
double screenHeight = [UIScreen mainScreen].bounds.size.height - 20; 

if(textOrigin > screenHeight - keyboardHeight) 
{ 
    double sofset = textOrigin - (screenHeight - keyboardHeight); 
    CGPoint offset = scrollBackGround.contentOffset; 
    offset.y += sofset; 
    [scrollBackGround setContentOffset:offset animated:YES]; 
} 

} 


- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 
    [scrollBackGround setContentOffset:CGPointMake(0, 0) animated:YES]; 
} 

在keyboardWasShown功能我们正在做的就是获取键盘的高度并检查textField的y轴(即函数中的textOrigin)是否更大在键盘的Y轴上滑动包含我们文本字段的滚动视图的内容。

NOw如何获得文本字段的Y轴。为此,您必须使用文本框的委托,对下列代表将触发时,你的文本字段将成为第一个响应者

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    textOrigin = scrollBackGround.frame.origin.y + textField.frame.origin.y + 20(it is status bar height) + yourNavigationBarheight; 

// Make sure to make textOrigin an ivar in your .h file 
} 

最后在keyboardWillBeHidden我们正在重置滚动视图内容查看

+0

感谢您的宝贵提示.................... – user1347963 2012-04-23 07:35:14

+0

高兴地帮助兄弟:) – superGokuN 2012-04-23 11:36:42