UILabel UITextField

来源:互联网 发布:js修改div background 编辑:程序博客网 时间:2024/06/07 11:20
//
//  AppDelegate.m
//  2day
//
//  Created by huangJC on 15-1-27.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate
- (void)dealloc {
    self.window = nil;
    self.bTextField = nil;
    self.aTextField = nil;
    self.dictionary = nil;
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    UILabel * aLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 100, 30)];
    aLabel.backgroundColor = [UIColor greenColor];
    aLabel.text = @"用户名:";
    aLabel.textAlignment = NSTextAlignmentCenter;
    [self.window addSubview:aLabel];
    [aLabel release];
    
    
    //三种方法访问别的方法中的空间
    // 1 设置  tag值, 通过  tag 值找
    // 2 设置成实例变量, 创建对象时给实例变量赋值
    // 3 设置成属性, 创建对象时, 通过调用setter 赋值
    self.aTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 20, 150, 30)];
    self.aTextField.borderStyle = UITextBorderStyleRoundedRect;
    _aTextField.backgroundColor = [UIColor grayColor];
    _aTextField.placeholder = @"请输入用户名";
    _aTextField.tag = 100;
    _aTextField.delegate = self;
    [self.window addSubview:_aTextField];
    [_aTextField release];
    
    
    UILabel * bLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 100, 30)];
    bLabel.text = @"密码:";
    bLabel.textAlignment = NSTextAlignmentCenter;
    bLabel.backgroundColor = [UIColor yellowColor];
    [self.window addSubview:bLabel];
    [bLabel release];
    

    
    self.bTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 100, 150, 30)];
    self.bTextField.placeholder = @"请输入密码";
    self.bTextField.secureTextEntry = YES;
    self.bTextField.tag = 101;
    self.bTextField.borderStyle = UITextBorderStyleRoundedRect;
    self.bTextField.delegate = self;
    [self.window addSubview:_bTextField];
    [self.bTextField release];
    
    
    UIButton * loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.frame = CGRectMake(40, 160, 60, 30);
    loginButton.backgroundColor = [UIColor orangeColor];
    [loginButton setTitle:@"login" forState:UIControlStateNormal];
    [loginButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    [self.window addSubview:loginButton];
    
    
    UIButton * enterButton = [UIButton buttonWithType:UIButtonTypeCustom];
    enterButton.frame = CGRectMake(180, 160, 60, 30);
    enterButton.backgroundColor = [UIColor orangeColor];
    
    //改变按钮内部文字的字体大小
    enterButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [enterButton titleShadowColorForState:UIControlStateHighlighted];
    [enterButton setTitleColor:[UIColor whiteColor]
                      forState:UIControlStateNormal];
    [enterButton setTitleColor:[UIColor purpleColor]
                      forState:UIControlStateHighlighted];
    [enterButton setTitle:@"enter"
                 forState:UIControlStateNormal];
    [self.window addSubview:enterButton];
    
    
    [loginButton addTarget:self
                    action:@selector(doRegister)
          forControlEvents:UIControlEventTouchDown];
    
    [enterButton addTarget:self
                    action:@selector(doEnter)
          forControlEvents:UIControlEventTouchDown];
    
    return YES;
}

- (void)doEnter {
    if ([_aTextField.text isEqual:[self.dictionary valueForKey:@"account"]]
        && [_bTextField.text isEqual:[self.dictionary valueForKey:@"password"]]) {
        NSLog(@"......程功");
    }else {
        NSLog(@"......失败");
    }
    
    _aTextField.text = nil;
    _bTextField.text = nil;
    [_aTextField resignFirstResponder];
    [_bTextField resignFirstResponder];
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示"
                                                         message:@"登陆成功"
                                                        delegate:nil
                                               cancelButtonTitle:@"确定"
                                               otherButtonTitles:@"取消", nil];
    [alertView show];
    [alertView release];
}

- (void)doRegister{
    
//    UITextField * userTF = (UITextField *)[self.window viewWithTag:100];
//    UITextField * passwordTF = (UITextField *)[self.window viewWithTag:101];
//    NSLog(@"name= %@, password = %@", userTF.text, passwordTF.text);
    
    NSLog(@"name= %@, password = %@", self.aTextField.text, self.bTextField.text);
    self.dictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.aTextField.text, @"account", self.bTextField.text, @"password", nil];
    NSLog(@"dic: = %@", self.dictionary);
//    _aTextField.text = @"";
//    _bTextField.text = @"";
    self.aTextField.text = nil;
    self.bTextField.text = nil;
    [_aTextField resignFirstResponder];
    [_bTextField resignFirstResponder];
    
    //弹出对话框(警告框), 给出提示信息
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示"
                                                         message:@"注册成功"
                                                        delegate:nil
                                               cancelButtonTitle:@"确定"
                                               otherButtonTitles:@"取消", nil];
    [alertView show];
    [alertView release];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    textField.textColor = [UIColor redColor];
    NSLog(@"aaaaaaaaa");
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

0 0
原创粉丝点击