Objective c 在代码中将方法分配给UITextField
我的视图底部有一个文本字段。当你点击编辑它时,键盘会把它盖起来,你看不到你正在输入什么。我想创建一个方法,每当编辑此文本字段时,该方法将重新调整视图 如果我通过interface builder声明了textfield,那么我知道我所要做的就是链接(ctrl单击然后拖动)它。但是我在代码中声明了textfield,所以我想知道如何为它分配一个方法 我想做的伪代码:Objective c 在代码中将方法分配给UITextField,objective-c,uitextfield,Objective C,Uitextfield,我的视图底部有一个文本字段。当你点击编辑它时,键盘会把它盖起来,你看不到你正在输入什么。我想创建一个方法,每当编辑此文本字段时,该方法将重新调整视图 如果我通过interface builder声明了textfield,那么我知道我所要做的就是链接(ctrl单击然后拖动)它。但是我在代码中声明了textfield,所以我想知道如何为它分配一个方法 我想做的伪代码: if (the text field is currently being edited){ call method: ad
if (the text field is currently being edited){
call method: adjust view
}
您必须为文本视图指定一个代理:
[textView setDelegate: self]
我假设,
self
是您的视图控制器,它需要符合。文本视图在开始编辑循环时将调用textViewShouldBeginEditing:
/textViewWillBeginEditing
。您必须为文本视图分配一个代理:
[textView setDelegate: self]
我假设,
self
是您的视图控制器,它需要符合。文本视图在开始编辑循环时将调用textViewShouldBeginEditing:
/textViewWillBeginEditing
。创建文本字段时,将其委托设置为self
[textField setDelegate:self];
然后实现委托方法
- (void)textFieldDidBeginEditing:(UITextField *)textField {
//adjust view here
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
//move view back to correct position here
}
您还可以注册UIKeyboardWillShow/UIKeyboardWillHide或UIKeyboardDidShow/UIKeyboardDidHide消息,并实现相应的功能。创建文本字段时,将其委托设置为self
[textField setDelegate:self];
然后实现委托方法
- (void)textFieldDidBeginEditing:(UITextField *)textField {
//adjust view here
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
//move view back to correct position here
}
您还可以注册UIKeyboardWillShow/UIKeyboardWillHide或UIKeyboardDidShow/UIKeyboardDidHide消息,并实现相应的功能。在.h中实现UIExtFieldDelegate,如下所示:
@interface ShippingInfoViewController:UIViewController
并确保将文本字段的委托设置为self
然后您可以访问许多方法,例如
-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串{
-(void)textfielddbeginediting:(UITextField*)textField{
-(BOOL)textField应该返回:(UITextField*)textField{
-(void)textfielddidediting:(UITextField*)textField{
还有很多其他的
在.h中实现UITextFieldDelegate,如下所示:
@interface ShippingInfoViewController:UIViewController
并确保将文本字段的委托设置为self
然后您可以访问许多方法,例如
-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串{
-(void)textfielddbeginediting:(UITextField*)textField{
-(BOOL)textField应该返回:(UITextField*)textField{
-(void)textfielddidediting:(UITextField*)textField{
还有很多其他的
使用以下功能:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
[self moveTextViewForKeyboard:notification up:YES];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
[self moveTextViewForKeyboard:notification up:NO];
}
- (void)moveTextViewForKeyboard:(NSNotification*)notification up:(BOOL)up {
NSDictionary *userInfo = [notification userInfo];
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
CGRect keyboardRect;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
if (up == YES) {
CGFloat keyboardTop = keyboardRect.origin.y;
CGRect newTextViewFrame = self.noteTextView.frame;
originalTextViewFrame = self.noteTextView.frame;
newTextViewFrame.size.height = keyboardTop - self.noteTextView.frame.origin.y - 10;
self.noteTextView.frame = newTextViewFrame;
} else {
// Keyboard is going away (down) - restore original frame
self.noteTextView.frame = originalTextViewFrame;
}
[UIView commitAnimations];
}
对originalTextViewFrame使用静态变量:
static CGRect originalTextViewFrame;
使用以下功能:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
[self moveTextViewForKeyboard:notification up:YES];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
[self moveTextViewForKeyboard:notification up:NO];
}
- (void)moveTextViewForKeyboard:(NSNotification*)notification up:(BOOL)up {
NSDictionary *userInfo = [notification userInfo];
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
CGRect keyboardRect;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
if (up == YES) {
CGFloat keyboardTop = keyboardRect.origin.y;
CGRect newTextViewFrame = self.noteTextView.frame;
originalTextViewFrame = self.noteTextView.frame;
newTextViewFrame.size.height = keyboardTop - self.noteTextView.frame.origin.y - 10;
self.noteTextView.frame = newTextViewFrame;
} else {
// Keyboard is going away (down) - restore original frame
self.noteTextView.frame = originalTextViewFrame;
}
[UIView commitAnimations];
}
对originalTextViewFrame使用静态变量:
static CGRect originalTextViewFrame;
在我的.h文件中,我的接口声明如下
@interface SecondDetailViewController:UIViewController
当我将文本字段的委托设置为自身时,它会在运行时崩溃,有什么想法吗?你是怎么做的?文本字段是在序列图像板中还是在代码中?我通常通过单击控件(或右键单击)在序列图像板中执行此操作从字段到文件所有者(底部的黄色框)并选择委派。在代码中,它应该是textField.delegate=self;
我发现了问题;我在分配前设置了委派。不过感谢您的响应。在我的.h文件中,当我设置textfi时,我的接口声明如下@interface SecondDetailViewController:UIViewController
eld对自身的委托在运行时崩溃,有什么想法吗?你是怎么做到的?文本字段是在情节提要中还是在代码中?我通常在情节提要中通过从字段到文件所有者(底部的黄色框)的控制单击(或右键单击)来完成此操作然后选择委派。在代码中,它应该是textField.delegate=self;
我发现了问题;我在分配前设置了委派。不过谢谢你的回答。@user1458968这是非常正确的;但是我怎么知道呢?我的答案是第一个:-)没关系。@user1458968这是非常正确的;但是我怎么知道呢现在呢?我的答案是第一个:-)没关系。