2018-02-25 iOS 登陆界面的简单编写(通过NSNotificationCenter)

//
//  ThirdViewController.m
//  text1
//
//  Created by xiaoxi  on 2018/1/11.
//  Copyright © 2018年  xiaoxi. All rights reserved.
//

#import "ThirdViewController.h"
#import "SliderButtonView.h"


@interface ThirdViewController ()
@property (nonatomic,strong) SliderButtonView *sliderButtonView;
@property (nonatomic,strong) UITextField *textField1;
@property (nonatomic,strong) UITextField *textField2;
@property (nonatomic,strong) UIButton *button;

@end

@implementation ThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"信息查询";
    self.view.backgroundColor = JMColor(245, 245, 245);
//    [self createUI];
    [self drawUI];
    // 通知中心 *注册* 观察者
    // 监听 123 频道消息
    // 主要作用不是传值,而是实现相隔较远的页面之间进行交互
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNotification:) name:@"123" object:nil];

}
/*
- (void)didReceiveNotification:(NSNotification *)sender{
    JMLog(@"这是一个通知");
}
#pragma mark - 移除通知中心的观察者(首选pop)
// 如果内存控制好的话,也可以在dealloc里面写,ARC下也可以写dealloc
- (void)dealloc
{
    // 移除所有的观察者
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    // 移除指定的观察者
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"123" object:nil];
}
*/

- (void)createUI{
    self.sliderButtonView = [[SliderButtonView alloc] initWithFrame:CGRectMake(0, 1 *KScale, kScreenWidth, 80 *KScale) WithButtonTitleArray:@[@"AAA",@"BBB",@"CCC"]];
    [self.view addSubview:self.sliderButtonView];
}

- (void)drawUI
{
    self.textField1 = [[UITextField alloc] init];
    self.textField1.borderStyle = UITextBorderStyleLine;
    [self.view addSubview:self.textField1];
    
    [self.textField1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40 *KScale);
        make.right.mas_equalTo(-40 *KScale);
        make.top.mas_equalTo(180 *KScale);
        make.height.mas_equalTo(80 *KScale);
    }];
    
    self.textField2 = [[UITextField alloc] init];
    self.textField2.borderStyle = UITextBorderStyleLine;
    [self.view addSubview:self.textField2];
    
    [self.textField2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.textField1).offset(0);
        make.right.mas_equalTo(self.textField1).offset(0);
        make.top.mas_equalTo(self.textField1).offset(100 *KScale);
        make.height.mas_equalTo(self.textField1);
    }];
    
    self.button = [[UIButton alloc] init];
    [self.button setTitle:@"登陆" forState:UIControlStateNormal];
    self.button.backgroundColor = [UIColor yellowColor];
    self.button.layer.cornerRadius = 5 *KScale;
    self.button.layer.borderWidth = 2 *KScale;
    [self.button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.button.enabled = NO;
    [self.button addTarget:self action:@selector(cliclkButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];
    
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.textField2).offset(140 *KScale);
        make.width.mas_equalTo(130 *KScale);
        make.height.mas_equalTo(60 *KScale);
        make.centerX.mas_equalTo(self.view.mas_centerX);
    }];
    
    [self NotificationCenter];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
//通知中心监测
- (void)NotificationCenter
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(texteDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
}
//实现按钮状态的改变
- (void)texteDidChange:(NSNotification*)notification
{
    if ([self.textField1.text isEqualToString:@""]||[self.textField2.text isEqualToString:@""]||self.textField2.text.length <6) {
        self.button.enabled = NO;
    }else{
        self.button.enabled = YES;
        [self.button setBackgroundColor:[UIColor blackColor]];
    }
}

- (void)cliclkButton
{
    JMLog(@"登录按钮点击");
}


@end

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

推荐阅读更多精彩内容