UI 03 UIButton 和 UITextField

可以将UIButton 与 UITextField 结合起来使用, 产生如下图的效果.

// 新建一个Button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(100, 300, 50, 50);
    button.backgroundColor = [UIColor cyanColor];
    [self.window addSubview:button];

    button.layer.borderWidth = 1;
    [button setTitle:@"显示" forState:UIControlStateNormal ];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    // 新建一个UITextField的属性.
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(70, 100, 120, 50)];
    self.textField.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:self.textField];
    [self.textField release];

    self.textField.textColor = [UIColor cyanColor];
    self.textField.layer.borderWidth = 1;
    self.textField.layer.cornerRadius = 10;
    self.textField.clearButtonMode = UITextFieldViewModeAlways;
    self.textField.placeholder = @"请输入密码";
    self.textField.secureTextEntry = YES;
    self.textField.keyboardType = UIKeyboardTypeNumberPad;
    self.textField.tag = 1000;

    // 他是UIControl的子类,可以添加addTag 方法
    [self.textField addTarget:self action:@selector(chageValue:) forControlEvents:UIControlEventEditingChanged];

    //创建一个按钮,用来切换状态
    UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
    newButton.frame = CGRectMake(60, 180, 40, 40);
    newButton.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:newButton];
    self.isSelect = NO;
    [newButton setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
    [newButton addTarget:self action:@selector(changePic:) forControlEvents:UIControlEventTouchUpInside];

    // 创建一个label来显示
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(110, 180, 150, 40)];
    label.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:label];
    [label release];

    label.text = @"是否显示密码";
    label.alpha = 0.5;

 //创建一个label
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(210, 110, 150, 30)];
    self.label.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:self.label];
    [_label release];
    self.label.alpha = 0.5;
    self.label.text = @"passWord lenth";

对应的方法:

- (void)click:(UIButton *)button{
//    UITextField *t = (UITextField *)[self.window viewWithTag:1000];
    NSLog(@"%@",self.textField.text);

}

- (void)changePic:(UIButton *)button{
    if (self.isSelect) {
        [button setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
    }else{
         [button setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
    }
    self.isSelect = !self.isSelect;
    self.textField.secureTextEntry = !self.textField.secureTextEntry;
}

- (void)chageValue:(UITextField *)textField{
    NSLog(@"%@",textField.text);
    if (textField.text.length >5) {
        self.label.text = @"密码长度适合";
    }else{
        self.label.text = @"密码长度太短";
    }

}

从图中还可以看到一个mybutton 字样的button.

那个是新建的继承于UIButton的MyButton 类.

  // MyButton类型的对象.
    MyButton *meButton = [MyButton buttonWithType:UIButtonTypeSystem];
    meButton.frame = CGRectMake(200, 250, 100, 100);
    meButton.buttonName = @"宋某人";
    [self.window addSubview:meButton];
    [meButton setTitle:@"myButton" forState:UIControlStateNormal];

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-19 08:41:56

UI 03 UIButton 和 UITextField的相关文章

UILabel、UIButton、UITextField控件的简单使用

UILabel.UIButton.UITextField是UI中最简单也是必须掌握的基础控件,下面我来介绍下这3个控件分别用来干嘛的. UILabel:是一个文本框,主要用来显示文本信息的,比如微博上看到的文字,就是UILabel. UIButton:是一个按钮,用来点击触发一些事件的.比如QQ上发送消息,就是UIButton. UITextField:是输入框,用来输入文本信息的.比如QQ上面打字. 话不多说,直接上代码.我们先来完成UILabel,为了使代码看起来更加美观,我们就把UILab

UI 03 关于UITextField键盘遮挡问题

首先,需要引头文件, 签订协议 已改成 MRC ! //1.三个textfield UITextField *textField1 = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 150, 40)]; textField1.layer.borderWidth = 1; textField1.layer.cornerRadius = 10; [self.view addSubview:textField1]; textField1.

UI 03 UITextField

UITextField 继承于 UIControl . 特殊的技能是可以输入. // 输入框 UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 50, 200, 40)]; textField.backgroundColor = [UIColor whiteColor]; [self.window addSubview:textField]; [textField release]; textF

UIKit框架(18)UIButton和UITextField

UIButton按钮控件和UITextField输入框控件,是UI开发中比较常用的两个控件 和UILabel.UIImageView.UISwitch相比,用法相对比较丰富 UIButton的四个状态 UIButton有四个状态: //正常状态 UIControlStateNormal  //高亮状态:当按钮被按下时的状态 UIControlStateHighlighted  //选中状态:通过UIButton对象的selected属性进行切换 UIControlStateSelected  /

UIButton,UIImageView,UITextField

1.UIButton的基本设置 1 //创建UIButton 2 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 3 button.frame = CGRectMake(150, 200, 70, 40); 4 button.backgroundColor = [UIColor redColor]; 5 /** 6 * 设置按钮标题 7 * 标题内容 8 * 按钮状态 9 */ 10 11 [button setT

UI控件(UITextField)

@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UITextField* textField1 = [[UITextField alloc] init]; //设置代理表示实现协议 UITextFieldDelegate textField1.delegate = self; textField1.frame = CGRectMake( 20, 120, 280, 30 ); // 边框样式 //

UI 03 UIViewController (视图控制器)的7个方法与视图的跳转

新建一个继承于UIViewController 的类 在AppDelegate.m 中写如下代码. //1.创建一个rootViewController对象 RootViewController *rootVC = [[RootViewController alloc]init]; //2.给window设置根视图控制器 self.window.rootViewController = rootVC; [rootVC release]; 在RootViewController.m文件中的代码如下

UI 03 LTView

LTView 是自写的继承于 UIView 的类 这其中创建一个UILabel 和一个 UITextField ; 这样可以少些一半的代码. 代码如下: LTView.h #import <UIKit/UIKit.h> @interface LTView : UIView<UITextFieldDelegate> // 因为要在类的外部获取输入框的内容,修改Label的标题,所以我们把这两部分作为属性写在.h这样在外部可以直接进行修改和设置 @property(nonatomic,

[UI基础]UIButton

UIButton继承关系如下: UIButton-->UIControl-->UIView-->UIResponder-->NSObject 1.创建一个UIButton对象 UIButton提供了如下类方法来创建一个指定类型的UIButton对象 1 + (void)buttonWithType:(UIButtonType)buttonType UIButtonType是一个枚举类型 1 typedef enum{ 2 UIButtonTypeCustom = 0; //此属性表