判断输入的用户名和密码是否是lanoukeji以及123456。如果是,弹出alertview提示用户“欢迎回来”,如果不是,提示“用户名或者密码错误,请核对后再试”,并给出两个按钮,确定和取消。

#import "ZXHAppDelegate.h"
@interface ZXHAppDelegate ()
{
    UIView *_containerView;
    
}
@end
@implementation ZXHAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    
    //登陆
    _containerView = [[UIView alloc]  initWithFrame:CGRectMake(0, 0, 320, 586)];
    _containerView.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:_containerView];
    [_containerView release];
    
    //登陆界面
    
    UILabel *label1 = [[UILabel alloc]  initWithFrame:CGRectMake(50, 50, 60, 50)];
    label1.backgroundColor = [UIColor whiteColor];
    label1.text = @"用户名:";
    label1.textAlignment = NSTextAlignmentRight;
    [_containerView addSubview:label1];
    [label1 release];
    
    UITextField *field1 = [[UITextField alloc]  initWithFrame:CGRectMake(130, 50, 
140, 50)];
    field1.backgroundColor = [UIColor whiteColor];
    field1.borderStyle = UITextBorderStyleRoundedRect;
    field1.placeholder = @"请输入用户名";
    field1.textAlignment = NSTextAlignmentCenter;
    field1.tag = 100;
    //协议
    field1.delegate = self;
    [_containerView  addSubview:field1];
    [field1 release];
    
    UILabel *label2 = [[UILabel alloc]  initWithFrame:CGRectMake(50, 120, 60, 50)];
    label2.backgroundColor = [UIColor whiteColor];
    label2.text = @"密码:";
    label2.textAlignment = NSTextAlignmentRight;
    [_containerView addSubview:label2];
    [label2 release];
    
    UITextField *field2 = [[UITextField alloc]  initWithFrame:CGRectMake(130, 120, 
140, 50)];
    field2.backgroundColor = [UIColor whiteColor];
    field2.borderStyle = UITextBorderStyleRoundedRect;
    field2.placeholder = @"请输入密码";
    field2.textAlignment = NSTextAlignmentCenter;
    field2.tag = 101;
    //协议
    field2.delegate = self;//在ZXHAppDelegate.h中加一个UITextFieldDelegate
    
    [_containerView addSubview:field2];
    [field2 release];
    
    [self setupButton];
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


//初始化button
- (void)setupButton
{
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn1.frame = CGRectMake(50, 200, 220, 50);
    btn1.backgroundColor = [UIColor greenColor];
    [btn1 setTitle:@"登陆" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(button:) forControlEvents:
UIControlEventTouchUpInside];
    [_containerView addSubview:btn1];
    
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn2.frame = CGRectMake(50, 300, 220, 50);
    btn2.backgroundColor = [UIColor greenColor];
    [btn2 setTitle:@"取消" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(btn2:) forControlEvents:
UIControlEventTouchUpInside];
    [_containerView addSubview:btn2];
    
}


- (void)button:(UIButton*)button
{
    //1.获取输入框
    UITextField *textField1 = (UITextField*)[button.superview viewWithTag:100];
    UITextField *textField2 = (UITextField*)[button.superview viewWithTag:101];
    //2.判断输入框文字的长度
    if ( [textField1.text isEqual: @"lanoukeji"] &&
 [textField2.text isEqual:@"123456"]) {
        NSLog(@"欢迎回来");
        UIAlertView *alertView = [[UIAlertView alloc]
 initWithTitle:@"密码正确" message:@"欢迎回来"
 delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
        [alertView show]; //让alertView弹出
        
        [alertView release];
        
    }else{
        //title: 提示框的标题
        //message:提示框的提示信息
        //delegate: 代理
        //cancelButtonTitle: 取消按钮显示的文字
        //otherButtonTitles: 其他按钮显示的文字,只给按钮显示的文字即可,可以有多个
        UIAlertView *alertView = [[UIAlertView alloc]  initWithTitle:@"警告"
 message:@"用户名或者密码错误,请核对后再试" delegate:self cancelButtonTitle:@"取消"
 otherButtonTitles:@"确定", nil];
        alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
        [alertView show]; //让alertView弹出
        
        [alertView release];
    }
}
- (void)btn2:(UIButton*)btn2
{
    NSLog(@"取消");
}

- (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:.
}
- (void)dealloc
{
    [_window release];
    [super dealloc];
}
@end


 

你可能感兴趣的:(UI)