一个简单的登录功能,遇到最大的问题就是怎么处理弹出的键盘,如何让它下去,这是一个首先要处理的问题。
ViewController.h:
#import <UIKit/UIKit.h> @interface DXWViewController : UIViewController - (IBAction)OKClick:(id)sender; - (IBAction)cancleClick:(id)sender; @property (retain, nonatomic) IBOutlet UILabel *label; - (IBAction)end:(id)sender;//键盘退出方法 @property (retain, nonatomic) IBOutlet UITextField *name; @property (retain, nonatomic) IBOutlet UITextField *password; @end
ViewController.m:
#import "DXWViewController.h" @interface DXWViewController () @end @implementation DXWViewController - (void)viewDidLoad { [super viewDidLoad]; //view一加载就让输入框成为第一响应者 [self.name becomeFirstResponder]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (IBAction)OKClick:(id)sender { NSString *strName = self.name.text; NSString *strPwd = self.password.text; self.label.text = [NSString stringWithFormat:@"姓名:%@,密码:%@",strName,strPwd]; //根据字的长度来设置label的大小,而不是用autolayerout来实现 CGSize size = [self.label.text sizeWithFont:self.label.font]; CGRect frame = CGRectMake(self.label.frame.origin.x,self.label.frame.origin.y, size.width, self.label.frame.size.height); self.label.frame = frame; } - (IBAction)cancleClick:(id)sender { } - (void)dealloc { [_label release]; [_name release]; [_password release]; [super dealloc]; } - (IBAction)end:(id)sender { [self.name resignFirstResponder]; [self.password resignFirstResponder]; } @end
时间: 2024-11-10 12:00:47