Button和Textfield 及其控件

来源:互联网 发布:知识竞赛软件免费版 编辑:程序博客网 时间:2024/05/18 03:37

@interfaceAppDelegate ()

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

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

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


点击按钮看到对应的文本

(1).创建按钮

UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];

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

button.backgroundColor = [UIColoryellowColor];

button.layer.borderWidth =1;

button.layer.cornerRadius =10;

[buttonsetTitle:@"测试"forState:UIControlStateNormal];

[self.windowaddSubview:button];


调用输出文本的方法

[buttonaddTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchUpInside];



(2).创建文本

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

self.textField.layer.borderWidth = 1;

self.textField.layer.cornerRadius = 10;

[self.windowaddSubview:self.textField];

[_textFieldrelease];



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

self.textField.secureTextEntry = YES;


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

[self.textFieldaddTarget:selfaction:@selector(changeValue:)forControlEvents:UIControlEventEditingChanged];


(5).创建Label

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

self.label.backgroundColor = [UIColorcyanColor];

[self.windowaddSubview:self.label];

[_labelrelease];



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:[UIImageimageNamed:@"checked.png"]forState:UIControlStateNormal];

    } else {

        [button setImage:[UIImageimageNamed:@"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
原创粉丝点击