iOS 中按钮随着编辑框输入的改变

关键代码如下:

#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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容