🌹输入框中有无内容时Button的颜色变化🌹

2018-10-02  本文已影响0人  JaneEyre3X

#import "ViewController.h"
#import "MyCellCollectionViewCell.h"
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    UITextField * text;
    UIButton * bt;
}
@property (nonatomic,strong) UIView *headerView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    text = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, self.view.frame.size.width-100, 30)];
    text.backgroundColor =[UIColor grayColor];
    [self.view addSubview:text];
    
    
    bt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    bt.frame = CGRectMake((self.view.frame.size.width-80)/2, 150, 80, 30);
    [bt setTitle:@"登陆" forState:UIControlStateNormal];
    [bt setTitleColor:[UIColor redColor] forState:0];
    bt.alpha = 0.5;
    bt.userInteractionEnabled = NO;
    [bt addTarget:self action:@selector(Btn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bt];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(change:) name:UITextFieldTextDidChangeNotification object:text];
}
-(void)change:(NSNotification *)notification
{
    if (text.text.length > 0) {
        bt.userInteractionEnabled = YES;
        [bt setTitleColor:[UIColor redColor] forState:0];
        bt.alpha = 1;
//        NSLog(@"内容大于0可以点击");
    }else
    {
        bt.userInteractionEnabled = NO;
        [bt setTitleColor:[UIColor redColor] forState:0];
        bt.alpha = 0.5;
//        NSLog(@"内容为空不可点击");
    }    
}
-(void)Btn:(UIButton * )sender
{
    if (text.text.length > 0)
    {
    
        NSLog(@"内容大于0可以点击");
    }
    else
    {
        
        NSLog(@"内容为空不可点击");
    }
}
@end
内容.gif
上一篇 下一篇

猜你喜欢

热点阅读