ui

 

#import "ViewController.h"

#import "Person.h"

 

@interface ViewController ()<UITextFieldDelegate, UIAlertViewDelegate>

{

    UIImageView *_imgView;

    NSMutableArray *_personsArray; //存放所有注册用户的信息

}

@end

 

@implementation ViewController

 

- (UIButton *)customButton:(NSString *)title frame:(CGRect)frame action:(SEL)action

{

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = frame;

    [btn setTitle:title forState:UIControlStateNormal];

    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];

    return btn;

}

 

- (void)dealloc

{

    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];

    //....

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    _personsArray = [[NSMutableArray alloc]init];

    

    _imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 100, 100)];

    _imgView.image = [UIImage imageNamed:@"image1"];

    //设置图片的动画数组,数组中存放的是UIImage的对象

    [_imgView setAnimationImages:@[[UIImage imageNamed:@"image1"], [UIImage imageNamed:@"image2"],[UIImage imageNamed:@"image3"], [UIImage imageNamed:@"image4"]]];

    //设置播放一次所需的时间

    _imgView.animationDuration = 1.2;

    //指定动画重复的次数

    _imgView.animationRepeatCount = 0;

    //开始动画

    [_imgView startAnimating];

    [self.view addSubview:_imgView];

    

    UIButton *stopBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    [stopBtn addTarget:self action:@selector(onClickedStop) forControlEvents:UIControlEventTouchUpInside];

    stopBtn.frame = CGRectMake(120, 20, 40, 40);

    [self.view addSubview:stopBtn];

    

    //文本框

    UITextField *tf1 = [[UITextField alloc]initWithFrame:CGRectMake(100, 150, 100, 24)];

    tf1.tag = 10;

    tf1.backgroundColor = [UIColor whiteColor];

    tf1.borderStyle = UITextBorderStyleNone;

//    tf1.text = @"iOS";

    tf1.textColor = [UIColor blueColor];     //文字颜色

    tf1.font = [UIFont systemFontOfSize:20]; //字体

    tf1.textAlignment = NSTextAlignmentCenter;

    tf1.placeholder = @"请输入";              //提示

    tf1.adjustsFontSizeToFitWidth = YES;

    tf1.minimumFontSize = 17;

//    tf1.background = [UIImage imageNamed:@"image1"];

    //disable状态下显示的图片

    tf1.disabledBackground = [UIImage imageNamed:@"image4"];

    //设置状态为disable

    tf1.enabled = YES;

    //开始编辑时清空

    tf1.clearsOnBeginEditing = YES;

    //当编辑的时候出现删除按钮

    tf1.clearButtonMode = UITextFieldViewModeWhileEditing;

    tf1.keyboardType = UIKeyboardTypeDefault;

    tf1.returnKeyType = UIReturnKeyDefault;

    //密文显示风格

    tf1.secureTextEntry = NO;

    tf1.delegate = self;

    NSLog(@"tf1:%@", tf1);

    

    //编辑改变事件

    [tf1 addTarget:self action:@selector(onEditChanged:) forControlEvents:UIControlEventEditingChanged];

    [self.view addSubview:tf1];

    

    UIButton *btn1 = [self customButton:@"stopEdit" frame:CGRectMake(220, 150, 100, 24) action:@selector(onStopEdit)];

    [self.view addSubview:btn1];

    

    UITextField *tf2 = [self customTextField:@"请输入用户名" frame:CGRectMake(10, 200, 180, 24) isSecure:NO];

    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeContactAdd];

    leftBtn.frame = CGRectMake(0, 0, 40, 40);

    tf2.leftView = leftBtn;

    tf2.leftViewMode = UITextFieldViewModeAlways;

    

    tf2.tag = 11;

    [self.view addSubview:tf2];

    

    UITextField *tf3 = [self customTextField:@"请输入密码" frame:CGRectMake(10, 250, 180, 24) isSecure:YES];

    tf3.tag = 12;

    [self.view addSubview:tf3];

    

    UIButton *registerBtn = [self customButton:@"注册" frame:CGRectMake(10, 280, 80, 24) action:@selector(onRegister:)];

    [self.view addSubview:registerBtn];

    

    UIButton *loginBtn = [self customButton:@"登录" frame:CGRectMake(100, 280, 80, 24) action:@selector(onLogin:)];

    [self.view addSubview:loginBtn];

    

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onTextChanged:) name:UITextFieldTextDidChangeNotification object:nil];

}

 

- (void)onEditChanged:(UITextField *)tf

{

    NSLog(@"tf:%@", tf.text);

    if (tf.text.length > 6) {

        tf.text = [tf.text substringWithRange:NSMakeRange(0, 6)];

        

    }

}

 

- (void)onTextChanged:(NSNotification *)notifier

{

    NSLog(@"text changed");

}

 

- (void)onRegister:(UIButton *)btn

{

    UITextField *loginTF = (UITextField *)[self.view viewWithTag:11];

    UITextField *passwordTF = (UITextField *)[self.view viewWithTag:12];

    Person *p = [[Person alloc]init];

    p.username = loginTF.text;

    p.password = passwordTF.text;

    BOOL flag = NO;

    for (Person *per in _personsArray) {

        if ([per.username isEqualToString:p.username]) {

            flag = YES;

            //弹出警示框

            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"用户已经存在" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", @"不确定", nil];

            alert.tag = 10;

            [alert show];

            break;  //一旦发现已经注册,则不用继续判断

//            UIAlertController

        }

    }

    if (flag == NO) {

        [_personsArray addObject:p];

    }

 

}

 

#pragma mark - alert delegate

 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    NSLog(@"index:%ld", buttonIndex);

    UITextField *loginTF = (UITextField *)[self.view viewWithTag:11];

    if (alertView.tag == 11) {

        [loginTF becomeFirstResponder]; //弹出键盘

    }

}

 

- (void)onLogin:(UIButton *)btn

{

    UITextField *loginTF = (UITextField *)[self.view viewWithTag:11];

    UITextField *passwordTF = (UITextField *)[self.view viewWithTag:12];

    

    [loginTF resignFirstResponder];

    [passwordTF resignFirstResponder];

    

    NSString *message;

    for (Person *p in _personsArray) {

        if ([p.username isEqualToString:loginTF.text]) {

            if ([p.password isEqualToString:passwordTF.text]) {

                message = @"登录成功";

            }else

            {

                message = @"密码错误";

            }

        }else{

            message = @"用户不存在";

        }

    }

    UIAlertView *loginAlert = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

    loginAlert.tag = 11;

    [loginAlert show];

}

 

- (UITextField *)customTextField:(NSString *)placeholder frame:(CGRect)frame isSecure:(BOOL)secure

{

    UITextField *tf = [[UITextField alloc]initWithFrame:frame];

    tf.placeholder = placeholder;

    tf.borderStyle = UITextBorderStyleRoundedRect;

    tf.secureTextEntry = secure;

    return tf;

}

 

#pragma mark - textField delegate

 

//是否允许编辑, 返回NO则不允许

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

    NSLog(@"%s", __func__);

    return YES;

}

 

//已经开始编辑

- (void)textFieldDidBeginEditing:(UITextField *)textField

{

    NSLog(@"%s", __func__);

    [_imgView stopAnimating];

}

 

//已经结束编辑

- (void)textFieldDidEndEditing:(UITextField *)textField

{

    NSLog(@"didEnd:%@", textField);

    NSLog(@"%s", __func__);

}

 

//是否可以结束编辑,为NO则不可以

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField

{

    return YES;

}

 

//让键盘的return健可以点击

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [textField resignFirstResponder]; //退出键盘

    return YES;

}

 

- (void)onStopEdit

{

    UITextField *tf = [self.view viewWithTag:10];

    [tf resignFirstResponder];  //将键盘退出

}

 

- (void)onClickedStop

{

    //停止动画

    [_imgView stopAnimating];

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

 

posted @ 2015-08-26 21:10  ios-C  阅读(186)  评论(0编辑  收藏  举报