美文网首页
iOS 中按钮随着编辑框输入的改变

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

作者: Wong大丑 | 来源:发表于2015-12-22 14:06 被阅读275次

    关键代码如下:

    #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

    相关文章

      网友评论

          本文标题:iOS 中按钮随着编辑框输入的改变

          本文链接:https://www.haomeiwen.com/subject/szamhttx.html