UI基础-UITextField    UIButton   delegate

UITextField
  • UITextField(输入框):是控制文本输入和显示的控件,在App中UITextField出现频率也比较高。
  • Ios系统借助虚拟键盘实现输入,当点击输入框,系统会自动调出键盘,方便你进一步操作。在你不需要输入的时候,可以使用回收键盘的方法,收回弹出的键盘。
  • UITextField和UILabel相比,UILabel主要用于文字显示,不能编辑,UITextField允许用户编辑文字(输入)
文本显示
 
 
外观控制
 
 
UIButton
  • UIButton(按钮):是响应用户点击的控件。在App中UIButton是出现频率很高的空间。
  • UIButton与UILabel、UITextField侧重点不同,侧重于处理点按。当然UIButton类也提供了一些方法控制按钮外观。
事件
 
外观控制
 
 
delegate
点return回收键盘步骤:
  1. 将AppDelete作为UITextField的delegate
  2. AppDelete.h文件接受UITextFieldDelegate协议
  3. AppDelete.m文件实现textFieldShouldReturn:方法
 
 
//UITextField工程
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 30)];
    textField.backgroundColor = [UIColor greenColor];
    [self.window addSubview:textField];
    //要显示的文本
    textField.text = @"账号";
    //显示文本内容的颜色
    textField.textColor = [UIColor redColor];
    //文本的对齐方式
    textField.textAlignment = NSTextAlignmentCenter;
    //文本字体
    textField.font = [UIFont fontWithName:@"Helvetica- Bold" size:20];
    //占位字符串
    textField.placeholder = @"请输入";
    //是否允许输入
    textField.enabled = YES;
    //是否开始输入的时候清空输入框内容
    textField.clearsOnBeginEditing = YES;
    //是否文字以圆点格式显示
    textField.secureTextEntry = YES;
    //弹出键盘的类型(枚举值)
    textField.keyboardType = UIKeyboardTypeASCIICapable;
    //键盘右下⾓return按钮类型(枚举值)
    textField.returnKeyType = UIReturnKeySend;
    //⾃定义输入视图(默认是键盘)
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    view.backgroundColor = [UIColor redColor];
    textField.inputView = view;
    //输⼊视图上方的辅助视图(默认nil)
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    view1.backgroundColor = [UIColor blueColor];
    textField.inputAccessoryView = view1;
    //边框样式(枚举值)
    textField.borderStyle = UITextBorderStyleRoundedRect;
    //清除按钮模式(枚举值)
    textField.clearButtonMode = UITextFieldViewModeAlways;
    //输⼊入框左视图
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
    leftView.backgroundColor = [UIColor purpleColor];
    textField.leftView = leftView;
    //左视图的显⽰模式
    textField.leftViewMode = UITextFieldViewModeAlways;
    //输入框右视图
    UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    rightView.backgroundColor = [UIColor purpleColor];
    textField.rightView = rightView;
    //右视图的显⽰模式
    textField.rightViewMode = UITextFieldViewModeAlways;
    //提示信息窗口
    UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请保护好你的账号密码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    [av show];
    [self.window makeKeyAndVisible];
    return YES;
}
 
//UIButton工程
#import "AppDelegate.h"
#define KRandom arc4random()%256/255.0
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self createButton];
    [self.window makeKeyAndVisible];
    return YES;
}
-(void)createButton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(50, 100, 60, 40);
    button.backgroundColor = [UIColor redColor];
    [self.window addSubview:button];
    //为按钮添加事件,指定按钮点击之后,执⾏targetaction方法
    [button addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    //设置指定状态下的标题
    [button setTitle:@"登录" forState:UIControlStateNormal];
    [button setTitle:@"coco" forState:UIControlStateSelected];
   
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    //获取指定状态下的标题
//    NSString *nomalTitle = [button titleForState:UIControlStateNormal];
   
    //设置指定状态下的前景图片
    [button setImage:[UIImage imageNamed:@"4])33F4VWI`Y3]TSK3UTHQ6.jpg"] forState:UIControlStateNormal];
    //获取指定状态下的前景图片
    UIImage *normalImage = [button imageForState:UIControlStateNormal];
    NSLog(@"%@",normalImage);  
}
-(void)login:(UIButton *)sender{
    NSLog(@"正在登录");
   
    //点击按钮更换背景颜色
    self.window.backgroundColor = [UIColor colorWithRed:KRandom green:KRandom blue:KRandom alpha:1];
   
    //移除按钮的点击事件
//    [sender removeTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
}
 
//Delegate工程
#import <UIKit/UIKit.h>
//第二步
//<协议>---遵守
@interface AppDelegate : UIResponder <UIApplicationDelegate,UITextFieldDelegate>

@property (strong, nonatomic) UIWindow *window;


@end
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
    textfield.backgroundColor = [UIColor greenColor];
    [self.window addSubview:textfield];
    //第一步
    //设置代理
    textfield.delegate = self;
    [self.window makeKeyAndVisible];
    return YES;
}
//第三步
//键盘回收
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
//程序运行的步骤
- (void)applicationWillResignActive:(UIApplication *)application {
    NSLog(@"-----将要取消活跃状态");
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"-----程序已经进入后台");
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSLog(@"-----程序已经进入后");
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
    NSLog(@"-----程序已经进⼊入活跃状态");
}
- (void)applicationWillTerminate:(UIApplication *)application {
    NSLog(@"-----结束运行");
}
@end

posted on 2015-09-08 16:31  龙仔阿宝  阅读(220)  评论(0编辑  收藏  举报

导航