关键代码如下:
#import "ViewController.h"@interface ViewController (){
UIButton *button;
UITextField *textField;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 200, 100, 100);
button.backgroundColor = [UIColor lightGrayColor];
[button addTarget:self action:@selector(BtnClicked:) forControlEvents:UIControlEventTouchUpInside];
button.userInteractionEnabled = YES;
[self.view addSubview:button];
textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, 100, 20)];
textField.delegate = self;
textField.backgroundColor = [UIColor lightGrayColor];
[textField addTarget:self action:@selector(textValueChanged:) forControlEvents:UIControlEventEditingChanged];
[self.view addSubview:textField];
}
- (void)BtnClicked:(UIButton *)sender {
NSLog(@"%@",textField.text);
}
- (void)textValueChanged:(UITextField *)sender {
if (textField.text.length >= 5) {
button.backgroundColor = [UIColor redColor];
button.userInteractionEnabled = YES;
} else if (textField.text.length < 5) {
button.backgroundColor = [UIColor lightGrayColor];
button.userInteractionEnabled = NO;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
网友评论