IOS-textfield textview

匿名 (未验证) 提交于 2019-12-03 00:09:02

原文链接: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的特色。


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

通过在xcode中拖拽。


经过上面操作,弹出的选项对话框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 {


}



#pragma mark -- 实现 UITextViewDelegate委托协议中一个方法,当UITextView中文字输入有变化的时候会被调用
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {




}

@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 {





}

#pragma mark -- 实现 UITextViewDelegate委托协议中一个方法,当UITextView中文字输入有变化的时候会被调用
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {


}

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


}

@end

4.1 添加UIButton并添加引用

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

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 {





}

-(void)onButtonClick{

}

@end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!