UITextFieldAndButton

//
// AppDelegate.m
// UILessonUITextFieldAndButton
//
// Created by dulinlin on 15-7-28.
// Copyright (c) 2015年 lanouhn. All rights reserved.
//

import “AppDelegate.h”

@interface AppDelegate ()

// 准备一个输入框
@property (nonatomic ,retain) UITextField *myTextField;

@property (nonatomic , retain) UITextField *myTextField1;
// 准备一个containView
@property (nonatomic , retain) UIView *containerView;

@end

@implementation AppDelegate

  • (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    // 根据window的frame来创建全屏大小的containerView
    // self.containerView = [[UIView alloc]initWithFrame:self.window.frame];
    // 根据屏幕大小来创建containerView的frame
    self.containerView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];

    // 用来把所有的控件添加到containerView上
    [self addViewsToContainerView];

    [self.window addSubview:self.containerView];
    [self.containerView release];

    return YES;

    }

pragma mark - 输入框的代理事件

// 点击键盘return键触发的事件
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
// NSLog(@”return 按钮被点击了”);
// 让一个确定输入框的键盘回收
// [self.myTextField resignFirstResponder];

// 让下一行输入框的键盘弹起

// [self.myTextField1 becomeFirstResponder];

// [self.myTextField1 resignFirstResponder];

if ([textField isEqual:self.myTextField]) {
    // 如果点的是上面一个输入框,应该让下面的输入框变成第一响应者
    [self.myTextField1 becomeFirstResponder];

}
if ([textField isEqual:self.myTextField1]) {
    // 如果点击的时第二个输入框,应该让(myTextField1)的键盘回收
    [self.myTextField1 resignFirstResponder];
}


return YES;

}

  • (void)textFieldDidBeginEditing:(UITextField *)textField
    {
    NSLog(@”输入框did开始编辑”);
    }

  • (void)textFieldDidEndEditing:(UITextField *)textField
    {
    NSLog(@”输入框did结束编辑”);
    }

  • (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
    NSLog(@”输入框should开始编辑”);
    return YES;
    }

  • (BOOL)textFieldShouldEndEditing:(UITextField *)textField
    {
    NSLog(@”输入框should结束编辑”);
    return YES;
    }

  • (BOOL)textField:(UITextField )textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString )string
    {
    NSLog(@”输入框正在编辑”);
    NSLog(@”textField.tex = %@” , textField.text);
    NSLog(@”location = %ld” , range.location);
    NSLog(@”length = %ld” , range.length);
    NSLog(@”string = %@” , string);

    char c = [string characterAtIndex:0];
    NSLog(@”字符:%c” , c);
    if ((c >= ‘0’) &&(c <= ‘9’)) {
    return NO;
    }

    return YES;
    }

pragma mark - 给containerView添加控件

  • (void)addViewsToContainerView
    {
    // 添加输入框
    self.myTextField = [[UITextField alloc]initWithFrame:CGRectMake(20 , 50 , 300, 30)];
    self.myTextField.backgroundColor = [UIColor lightGrayColor];
    [self.containerView addSubview:self.myTextField];
    [self.myTextField release];

    // 设置输入框的提示字符(输入框的占位符)
    self.myTextField.placeholder = @”请输入用户名”;

    // 输入框的隐式输入法 (密码圆点输入模式)(默认是NO)
    // self.myTextField.secureTextEntry = YES;

    // 用户交互属性(YES)
    // self.myTextField.userInteractionEnabled = NO;

    // 改变输入框return键的类型
    // self.myTextField.returnKeyType = UIReturnKeySearch;
    self.myTextField.returnKeyType = UIReturnKeyNext;

    // 输入框的键盘类型
    // self.myTextField.keyboardType = UIKeyboardTypePhonePad;

    // 给输入框设置代理对象
    self.myTextField.delegate = self;

    // 输入框编辑是清空的属性(默认是NO)
    // self.myTextField.clearsOnBeginEditing = YES;

    // 输入框中清空按钮的模式
    // self.myTextField.clearButtonMode = UITextFieldViewModeUnlessEditing;

    // 自定义键盘(只有高度管用,其他的都不管用)
    // UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 300)];
    // view1.backgroundColor = [UIColor redColor];
    //
    // self.myTextField.inputView = view1;

    // 辅助键盘
    // UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];
    // view1.backgroundColor = [UIColor redColor];
    // self.myTextField.inputAccessoryView = view1;

    // 输入框左右视图
    // UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(10 , 10 , 50, 30)];
    // view1.backgroundColor = [UIColor redColor];
    // self.myTextField.leftView = view1;
    // self.myTextField.leftViewMode = UITextFieldViewModeAlways;

    // 贴第二个输入框
    self.myTextField1 = [[UITextField alloc]initWithFrame:CGRectMake(20, 90, 300, 30)];
    self.myTextField1.backgroundColor = [UIColor lightGrayColor];
    // 提示 占位符
    self.myTextField1.placeholder = @”请输入密码”;
    self.myTextField1.delegate = self;
    [self.containerView addSubview:self.myTextField1];

    [self.myTextField1 release];

    // 创建一个button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    // button 设置frame
    // button.frame = CGRectMake(100, 300, 100, 30);
    [button setFrame:CGRectMake(100, 300, 100, 30)];

    // 给button设置一个title
    [button setTitle:@”确定” forState:UIControlStateNormal];

    // 给button添加一个点击事件
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    UIImage *image = [UIImage imageNamed:@”RK`_900(WTB[EOYCR0PD)JK.jpg”];
    [button setImage:image forState:UIControlStateNormal];

// // button设置边框
// button.layer.borderColor = [[UIColor orangeColor]CGColor];
//
// // button设置边框宽度
// button.layer.borderWidth = 1;
//
// // 边框的圆角属性
// button.layer.cornerRadius = 7;

// 创建一个图片对象

// UIImage *image = [UIImage imageNamed:@”屏幕快照 2015-07-28 下午3.32.32.png”];
//
// UIImage *img = [UIImage imageNamed:@”RK`_900(WTB[EOYCR0PD)JK.jpg”];
//
// // 把图片对象贴到button的背景上面
// [button setBackgroundImage:image forState:UIControlStateHighlighted];
// [button setBackgroundImage:img forState:UIControlStateNormal];

[self.containerView addSubview:button];
// 要不要释放(一定不要释放,不是alloc出来的)

}

pragma mark - button点击事件的方法

  • (void)buttonAction:(UIButton *)sender
    {
    NSLog(@”%@” ,sender.titleLabel.text);
    // 拿到button上现实的字符串
    NSString *str = sender.titleLabel.text;
    // 让该字符串显示到第一个输入框里
    self.myTextField.text = str;
    NSLog(@”button被点击了”);
    }

  • (void)applicationWillResignActive:(UIApplication *)application {
    // 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 {
    // 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 {
    // 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 {
    // 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 {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

  • (void)dealloc
    {
    [_myTextField1 release];
    [_containerView release];
    [_myTextField release];
    [super dealloc];
    }

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【项目资源】:包含前端、后端、移动开发、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源,毕业设计等各种技术项目的源码。包括C++、Java、python、web、C#、EDA等项目的源码。 【适用人群】:适用于希望学习不同技术领域的初学者或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。 【项目质量】:所有源码都经过测试,可以直接运行。功能在确认正常工作后才上传。 【项目资源】:包含前端、后端、移动开发、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源,毕业设计等各种技术项目的源码。包括C++、Java、python、web、C#、EDA等项目的源码。 【适用人群】:适用于希望学习不同技术领域的初学者或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。 【项目质量】:所有源码都经过测试,可以直接运行。功能在确认正常工作后才上传。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值