IOS-textfield textview

原文链接:https://blog.csdn.net/weixin_36709064/article/details/89256779

1、介绍

UITextView显示多行文本视图,接受编辑输入,类似于文本编辑器,获取焦点的时候会从底部弹出软键盘输入

UITextField类似输入框,比如可用作search输入框,接受文本输入,获取焦点的时候会从底部弹出软键盘输入

2 Delegate
Delegate委托协议,有点类似于对接口类的实现,比如Android中Activity实现View.OnClickListener,当有Class实现了View.OnClickListener,那么就需要实现其接口方法onClick(View v),这里Delegate中也会存在相应的回调函数,比如UITextViewDelegate和UITextFieldDelegate各字都有自己可回调的函数。这是通过协议的方式给视图添加回调是IOS的特色。
 

3 UITextView 和 UITextViewDelegate
要给UITextView添加响应事件可以通过UITextViewDelegate协议来进行,那么怎样将UITextView的响应事件同UITextViewDelegate的回调函数相关连起来呢?注意如果不关联是无法回调到对应的函数的。
 3.1 Interface Builder实现

通过在xcode中拖拽。

见鼠标放在UITextField或者UITextView,右键点击(mac触摸板是双指点击)弹出对话框,选择delegate,将光标点按在后面⭕️上,这时候⭕️中间会出现一个“+”,然后拖拽到右边的View Controller上,这时候View Controller上会有一个蓝色这框表示它被选中了,然后松手
 

经过上面操作,弹出的选项对话框delegate选项后面的空心圆圈就变成实心了,如下图,表示delegate和View Controller建立了绑定关系

上面用的是UITextField做的例子,UITextView是相同操作

源码代码如下

#import "ViewController.h"
 
@interface ViewController ()<UITextFieldDelegate, UITextViewDelegate>
 
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIButton *button;
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
 
 
 
#pragma mark -- 实现 UITextViewDelegate委托协议中一个方法,当UITextView中文字输入有变化的时候会被调用
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    NSLog(@"textView 被调用了,变化的部分是 %@", text);
 
    return YES;
}
 
@end
3.2 通过代码添加

上面掩饰了通过非代码的方式让UITextView 和 UITextViewDelegate相关联,然后UITextViewDelegate中的某一个回调函数被调用了,下面将通过代码的方式将让UITextView 和 UITextViewDelegate相关联,

1)首先取消掉UITextView 和 UITextViewDelegate的的关系

选中Main.storyboard中的TextView,mac触摸板双指点击弹出如下选择框,点击红色小框中的X,取消掉绑定关系
 

2)在代码中添加UITextView 和 UITextViewDelegate的关联关系

这句话将UITextView和UITextViewDelegate协议关联:self.textView.delegate = self;

可以前后做一下对比实验,去掉这句话之后看在控制台是否有对应的输出。
  
#import "ViewController.h"
 
@interface ViewController ()<UITextFieldDelegate, UITextViewDelegate>
 
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    forControlEvents:UIControlEventTouchDown];
    self.textView.delegate = self;//这句话将UITextView和UITextViewDelegate协议关联
    self.textField.delegate = self;//这句话将UITextField和UITextFieldDelegate协议关联
}
 
#pragma mark -- 实现 UITextViewDelegate委托协议中一个方法,当UITextView中文字输入有变化的时候会被调用
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSLog(@"textView 被调用了,变化的部分是 %@", text);
    return YES;
}
 
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    NSLog(@"UITextField retrun 按键被调用了");
    return YES;
}
 
@end

4  Button的点击事件

4.1 添加UIButton并添加引用

那么在IOS开发中使用OC是怎么给一个UIView添加事件的呢?

1)在故事板中添加一个UIButton 

2)然后在ViewController.m中添加UIButton的引用

方式一

先在代码中手写@property (weak, nonatomic) IBOutlet UIButton *button;然后将其前面的小圆圈

这句话需要跟故事板中的Button绑定起来,通过拖拽的连线的方式,选中上面添加的代码前的小圆点到故事板中的button视图,如下图:
 

方式二

让xcode自动生成,按住control键,按照下图的箭头的路线拖拽到小框的位置,只要在@interface ViewController和@end之间即可,不一定非要是图中小红框标示的位置,然后在Name框后面输入,比如button,然后点击Connect按钮,就会自动生成方法一中手写的代码
 

下面是通过代码使用button

#import "ViewController.h"
 
@interface ViewController ()<UITextFieldDelegate, UITextViewDelegate>
 
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIButton *button;
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self.button addTarget:self action:@selector(onButtonClick) forControlEvents:UIControlEventTouchDown];
 
}
 
-(void)onButtonClick{
    NSLog(@"########## onButtonClick ");
}
 
@end
 

5、键盘的关闭

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{    NSLog(@"textview 被调用了%@",text);

 if([text isEqualToString:@"\n"]){    

   NSLog(@"onclick return");    

  [textView resignFirstResponder];    

   return NO;

   }  

 return YES;

}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{  

  NSLog(@"textField 被调用return");  

 [textField resignFirstResponder];    

return YES;

}

键盘的设置

发布了44 篇原创文章 · 获赞 9 · 访问量 8521

猜你喜欢

转载自blog.csdn.net/manmanlu2006/article/details/101053783