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

2015-12-22  本文已影响275人  Wong大丑

关键代码如下:

#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

上一篇下一篇

猜你喜欢

热点阅读