UI - UITextField和UIButton

来源:互联网 发布:金税盘备份的数据 解压 编辑:程序博客网 时间:2024/05/17 01:57
(#AppDelegate.h)#import <UIKit/UIKit.h>#warning 2.接收UITextFieldDelegate协议@interface AppDelegate : UIResponder <UIApplicationDelegate,UITextFieldDelegate >@property (strong, nonatomic) UIWindow *window;@end(#AppDelegate.m)#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (void)dealloc{    [_window release];    [super dealloc];}- (void)haha:(UIButton *)button{    button.selected = !button.selected;//    [button setTitle:@"点你妹" forState:(UIControlStateSelected)];    // 需求    // 用户输入完毕之后 点击button 键盘回收    // 键盘回收的几个方法    //1>设置textField的enable属性为NO.    //1.不断的找子视图 直到找到textField    //2.将textField设置为属性或者实例变量    //3.通过tag值    UIView *textField = [self.window viewWithTag:100];//    UITextField *textField = self.window.subviews[1];//    textField.enabled = !textField.enabled;    //2>让textField处于非编辑状态//    UITextField *textField = self.window.subviews[1];//    [textField endEditing:YES];    //3>让window处于非编辑状态//    [self.window endEditing:YES];    //4>让textField取消第一响应者//    UITextField *textField = self.window.subviews[1];    [textField resignFirstResponder];    //成为第一响应者//    [textField becomeFirstResponder];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    NSLog(@"%s ----- %d", __FUNCTION__, __LINE__);    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];    // Override point for customization after application launch.    self.window.backgroundColor = [UIColor lightGrayColor];    [self.window makeKeyAndVisible];    // UITxetField: 文本输入框    // 创建一个textField    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(30, 30, 250, 30)];    textField.backgroundColor = [UIColor lightGrayColor];    textField.placeholder = @"��"; //(灰色的请输入账号/密码)    textField.secureTextEntry = YES; // (密码小黑点)//    textField.keyboardType = UIKeyboardTypeNumberPad; // 弹出键盘样式    textField.returnKeyType = UIReturnKeyGo; // return键样式    // 自动首字母大写    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;    // 自动修正功能    textField.autocorrectionType = UITextAutocorrectionTypeNo;    //1.按钮随keyborad抬高,keyboard消失,按钮落回原处//    2. NSAttributedString    //3.label自适应高度.    // 修改textFeild弹出来的视图    UILabel *t = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 100)];    textField.inputView = t;    [t release];    UIView *accessoryView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];    accessoryView.backgroundColor =[UIColor clearColor];    textField.inputAccessoryView = accessoryView;    // 边框样式    textField.borderStyle = UITextBorderStyleRoundedRect;    // 总是显示清除按钮    textField.clearButtonMode = UITextFieldViewModeAlways;    // 给textField添加左视图    UILabel *laber = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 30)];    laber.text = @"用户名";    textField.leftView = laber;    [laber release];    textField.leftViewMode = UITextFieldViewModeAlways;    [self.window addSubview:textField];    [textField release];    // UIButton       // 1.UIButton的父类是谁 ==> UIContorl       // 2.创建一个button对象       // 3.如何给button添加事件    // 1>创建一个button对象(初始化方法 便利构造器)    // 如果button添加图片要用custom, system会将图片渲染    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];    // 2>设置button相关属性    button.frame = CGRectMake(30, 30, 80 , 30);    button.backgroundColor = [UIColor blackColor];    [button setTitle:@"登录" forState:(UIControlStateNormal)];    // 处于高亮状态时 显示呵呵    [button setTitle:@"呵呵" forState:(UIControlStateHighlighted)];    // 点击结束之后 显示"点你妹"//    button.selected = NO;//    button.selected = !button.selected;    [button setTitle:@"点你妹" forState:(UIControlStateSelected)];    [button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];//    [button setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];//    [button setBackgroundImage:[UIImage imageNamed:@"1"] forState:(UIControlStateNormal)];    // 给button添加点击事件//      三个参数的含义如下所示//         1.target:目标.即让谁准备去执行对应的方法//         2.action:动作.即target所要执行的方法名是什么//         3.controlEvent:事件控制.即在什么情况下让target去执行action//    [button addTarget:self action:@selector(haha:) forControlEvents:(UIControlEventTouchUpInside)];    // 3> 添加父视图    [self.window addSubview:button];    // 4> 释放内存//    [button release]; //因为是便利构造器所以不需要    /*    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(100 , 100, 250, 30)];    textField.backgroundColor = [UIColor whiteColor];    textField.placeholder = @"请输入"; //(灰色的请输入账号/密码)    textField.textColor = [UIColor blackColor];    textField.borderStyle = UITextBorderStyleRoundedRect;    textField.clearButtonMode = UITextFieldViewModeAlways;    textField.tag = 100;     */    // 点击键盘上的return按钮时让键盘回收#warning 1. 设置Delegate对象//    textField.delegate = self;//    //    //    [self.window addSubview:textField];    UITextField *textField1 = [[UITextField alloc]initWithFrame:CGRectMake(100 , 150, 250, 30)];    textField1.backgroundColor = [UIColor whiteColor];    textField1.placeholder = @"请输入"; //(灰色的请输入账号/密码)    textField1.textColor = [UIColor blackColor];    textField1.borderStyle = UITextBorderStyleRoundedRect;    textField1.clearButtonMode = UITextFieldViewModeAlways;    textField1.delegate = self;    [self.window addSubview:textField1];    [textField1 release];    [textField1 addTarget:self action:@selector(changeText:) forControlEvents:     UIControlEventEditingChanged];    return YES;}- (void)changeText:(UITextField *)textField{    NSLog(@"开始啦~~");}#warning 3.实现协议中的方法- (BOOL)textFieldShouldReturn:(UITextField *)textField{    [textField resignFirstResponder];    return YES;}- (void)applicationWillResignActive:(UIApplication *)application {    NSLog(@"%s ----- %d", __FUNCTION__, __LINE__);    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application {    NSLog(@"%s ----- %d", __FUNCTION__, __LINE__);    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application {    NSLog(@"%s ----- %d", __FUNCTION__, __LINE__);    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application {    NSLog(@"%s ----- %d", __FUNCTION__, __LINE__);    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application {    NSLog(@"%s ----- %d", __FUNCTION__, __LINE__);    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end
0 0