Button和Textfield 及其控件

@interface AppDelegate ()

@property(nonatomic, retain)UITextField *textField;(因为textField在方法和程序中都要用到所以定义这个类)

@property(nonatomic, assign)BOOL isClick;// (4).

@property(nonatomic, retain)UILabel *label;// (10).


点击按钮 看到对应的文本

(1).创建按钮

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

button.frame = CGRectMake(150, 300, 100, 40);

button.backgroundColor = [UIColor yellowColor];

button.layer.borderWidth = 1;

button.layer.cornerRadius = 10;

[button setTitle:@"测试" forState:UIControlStateNormal];

[self.window addSubview:button];


调用输出文本的方法

[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];



(2).创建文本

self.textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 100, 100, 40)];

self.textField.layer.borderWidth = 1;

self.textField.layer.cornerRadius = 10;

[self.window addSubview:self.textField];

[_textField release];



(3).设置输入进去之后是圆点(输入密码)

self.textField.secureTextEntry = YES;


(4).给textField添加addTarget-action方法(即时打印方法)

[self.textField addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventEditingChanged];


(5).创建Label

self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 400, 150, 40)];

self.label.backgroundColor = [UIColor cyanColor];

[self.window addSubview:self.label];

[_label release];



addTarget-action方法(即时打印方法)

- (void)changeValue:(UITextField *)textField

{

    NSLog(@"%@", textField.text);

    if (textField.text.length > 5) {

        self.label.text = @"密码长度可以";

    } else {

        self.label.text = @"密码长度过短";

    }

}


切换图片的方法

- (void)changePic:(UIButton *)button

{

    if (self.isClick) {

        [button setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];

    } else {

        [button setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];

        setBackgroundImage会将图片填充满边框(无论原来的图片有多大)

        setImage只是原图片的大小

    }

    self.isClick = !self.isClick;

    

    self.textField.secureTextEntry = !self.textField.secureTextEntry;圆点与非圆点的循环

}


输出文本的方法

- (void)click:(UIButton *)button

{

    NSLog(@"%@", self.textField.text);

}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值