//
// ViewController.m
// UI-隐藏键盘
//
// Created by NorthCity on 15/9/6.
// Copyright (c) 2015年 Tcg. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *num1TextField;
@property (weak, nonatomic) IBOutlet UITextField *num2TextField;
@property (weak, nonatomic) IBOutlet UILabel *sumLable;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//ViewController成为代理
self.num1TextField.delegate = self ;
self.num2TextField.delegate = self;
}
- (IBAction)clickSumButton {
NSString *num1String = self.num1TextField.text;
NSString *num2String = self.num2TextField.text;
int sum = [num1String intValue] + [num2String intValue];
self.sumLable.text = [NSString stringWithFormat:@"%d",sum];
// 第一种隐藏键盘取消第一响应者(但是此方法当响应者对象较多时候不适用)
// [self.num1TextField resignFirstResponder];
// [self.num2TextField resignFirstResponder];
// 第二种方法是结束所有子控件的编辑
[self.view endEditing:YES];
}
// 第三种方法是点击空白区域隐藏键盘
// touchesBegan是一个空白区域监听方法,类似方法有四个
/*
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
*/
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
//第四种方法是点击return 隐藏键盘
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[self.view endEditing:YES];
return YES;
}
@end
UI基础-01 隐藏键盘
最新推荐文章于 2024-08-05 23:02:44 发布