iOS之UITextView的使用——使用导航栏关闭虚拟键盘

1、.h

#import <UIKit/UIKit.h>

@interface FKViewController : UIViewController <UITextViewDelegate>
@property (strong, nonatomic) IBOutlet UITextView *textView;

@end

2、.m

#import "FKViewController.h"

@interface FKViewController ()
@property (nonatomic, strong) UINavigationItem* navItem;
@end

@implementation FKViewController
UIBarButtonItem* done;
- (void)viewDidLoad
{
	[super viewDidLoad];
	// 将该控制器本身设置为textView控件的委托对象
	self.textView.delegate = self;
	// 创建、并添加导航条
	UINavigationBar* navBar = [[UINavigationBar alloc] initWithFrame:
		CGRectMake(0, 20, 320, 44)];
	[self.view addSubview:navBar];
	// 创建导航项、并设置导航项的标题
	self.navItem = [[UINavigationItem alloc]
		initWithTitle:@"导航条"];
	// 将导航栏添加到导航项中
	navBar.items = [NSArray arrayWithObject:self.navItem];
	// 创建一个UIBarButtonItem对象,并赋给done属性
	done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
		target:self action:@selector(finishEdit)];
}

- (void)didReceiveMemoryWarning
{
	[super didReceiveMemoryWarning];
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
	// 为导航条设置右边的按钮
	self.navItem.rightBarButtonItem = done;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
	// 取消导航条设置右边的按钮
	self.navItem.rightBarButtonItem = nil;
}
- (void) finishEdit {
	// 让textView控件放弃作为第一响应者
	[self.textView resignFirstResponder];
}
@end

使用storyboard布局

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值