注册登录页面跳转

AppDelegate.h

//
//  AppDelegate.m
//  LoginAndRegister1
//
//  Created by chenshunyi on 2017/12/11.
//  Copyright © 2017年 house365. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    ViewController *vc = [[ViewController alloc] init];
    //创建一个导航控制器容器 并且把viewController作为第一个根试图控制器放倒容器中
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    //然后把这个容器放到window的跟试图控制器上去展示出来
    self.window.rootViewController = nav;

    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 invalidate graphics rendering callbacks. 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 active 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

ViewController.m

//
//  ViewController.m
//  LoginAndRegister1
//
//  Created by chenshunyi on 2017/12/11.
//  Copyright © 2017年 house365. All rights reserved.
//

#import "ViewController.h"
#import "LoginViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor yellowColor];


    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(110, 100, 100, 50);
    [button setTitle:@"去登陆" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

}

-(void)buttonAction:(UIButton *)sender{
    LoginViewController *loginVC = [[LoginViewController alloc] init];

    //用导航控制器"压栈"的方式跳转到登录页面   相对应的名词叫做"出栈"  pop或者popTo
    [self.navigationController pushViewController:loginVC animated:YES];


    //直接让需要展示的界面展示到用户的视线中  没有压栈过程   所以不出出现导航栏
//    [self presentViewController:loginVC animated:YES completion:nil];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

LoginViewController.m

//
//  LoginViewController.m
//  LoginAndRegister1
//
//  Created by chenshunyi on 2017/12/11.
//  Copyright © 2017年 house365. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"

@interface LoginViewController ()<UIAlertViewDelegate>{

    UITextField *_nameTF;
    UITextField *_pswTF;
}

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

//    [self.navigationController popViewControllerAnimated:<#(BOOL)#>]

    //设置导航栏的标题
    self.title = @"登录界面";


    UILabel *nameLab = [[UILabel alloc] initWithFrame:CGRectMake(12, 100, 60, 25)];
    nameLab.text = @"用户名:";
    nameLab.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:nameLab];

    _nameTF = [[UITextField alloc] initWithFrame:CGRectMake(82, 100, 150, 25)];
    _nameTF.placeholder = @"请输入用户名";
    _nameTF.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_nameTF];


    UILabel *pswLab = [[UILabel alloc] initWithFrame:CGRectMake(12, 150, 60, 25)];
    pswLab.text = @"密码:";
    pswLab.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:pswLab];

    _pswTF = [[UITextField alloc] initWithFrame:CGRectMake(82, 150, 150, 25)];
    _pswTF.placeholder = @"请输入密码";
    _pswTF.borderStyle = UITextBorderStyleRoundedRect;
    _pswTF.secureTextEntry = YES;
    [self.view addSubview:_pswTF];



    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.frame = CGRectMake(60, 200, 200, 40);
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    loginButton.backgroundColor = [UIColor orangeColor];
    loginButton.tag = 0;
    [loginButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];


    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    registerButton.frame = CGRectMake(60, 265, 200, 40);
    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
    registerButton.backgroundColor = [UIColor orangeColor];
    registerButton.tag = 1;
    [registerButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerButton];

}
-(void)buttonAction:(UIButton *)sender{
    if (sender.tag == 0) {//登录按钮
        //获取输入框内的用户名和密码
        NSString *nameStr = _nameTF.text;//获取用户名文本框中输入的信息
        NSString *pswStr = _pswTF.text;//获取密码文本框中输入的信息

        //找到沙盒"单例"
        NSUserDefaults *user_d = [NSUserDefaults standardUserDefaults];


        //取出注册成功时存储的账号密码
        NSString *name = [user_d objectForKey:@"name"];
        NSString *psw = [user_d objectForKey:@"psw"];

        //校验账号
        if (![nameStr isEqualToString:name]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"账号输入错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
            [alert show];
            return;
        }

        if (![pswStr isEqualToString:psw]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"密码输入错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
            [alert show];
            return;
        }

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"登录成功" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        alert.tag = 1;
        [alert show];






    }else if (sender.tag == 1){//注册按钮

        //创建一个注册页面
        RegisterViewController *registerVC = [[RegisterViewController alloc] init];


        [self.navigationController pushViewController:registerVC animated:YES];


    }
}
//alertView的按钮点击事件alertView:当前弹框view   buttonIndex:点击的是第几个按钮(从左往右一次是0,1,2...)
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ((buttonIndex == 1) && (alertView.tag == 1)) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
//
//  RegisterViewController.m
//  LoginAndRegister1
//
//  Created by chenshunyi on 2017/12/11.
//  Copyright © 2017年 house365. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()<UIAlertViewDelegate>{

    UITextField *_nameTF;
    UITextField *_pswTF;
    UITextField *_pswTF2;

}

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    //设置导航栏的标题
    self.title = @"注册界面";


    UILabel *nameLab = [[UILabel alloc] initWithFrame:CGRectMake(12, 100, 60, 25)];
    nameLab.text = @"用户名:";
    nameLab.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:nameLab];

    _nameTF = [[UITextField alloc] initWithFrame:CGRectMake(82, 100, 150, 25)];
    _nameTF.placeholder = @"请输入用户名";
    _nameTF.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_nameTF];


    UILabel *pswLab = [[UILabel alloc] initWithFrame:CGRectMake(12, 150, 60, 25)];
    pswLab.text = @"密码:";
    pswLab.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:pswLab];

    _pswTF = [[UITextField alloc] initWithFrame:CGRectMake(82, 150, 150, 25)];
    _pswTF.placeholder = @"请输入密码";
    _pswTF.borderStyle = UITextBorderStyleRoundedRect;
    _pswTF.secureTextEntry = YES;//文本是否加密
    [self.view addSubview:_pswTF];


    UILabel *pswLab2 = [[UILabel alloc] initWithFrame:CGRectMake(12, 200, 60, 25)];
    pswLab2.text = @"确认密码:";
    pswLab2.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:pswLab2];

    _pswTF2 = [[UITextField alloc] initWithFrame:CGRectMake(82, 200, 150, 25)];
    _pswTF2.placeholder = @"请确认密码";
    _pswTF2.borderStyle = UITextBorderStyleRoundedRect;
    _pswTF2.secureTextEntry = YES;
    [self.view addSubview:_pswTF2];


    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    registerButton.frame = CGRectMake(60, 250, 200, 40);
    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
    registerButton.backgroundColor = [UIColor orangeColor];
    [registerButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerButton];


}

-(void)buttonAction:(UIButton *)sender{
    [self.view endEditing:YES];
    if ([_nameTF.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"用户名不能为空" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        [alert show];
        return;
    }

    if ([_pswTF.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"密码不能为空" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        [alert show];
        return;
    }

    if (![_pswTF2.text isEqualToString:_pswTF.text]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"两次密码输入不匹配" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        [alert show];
        return;
    }

    //创建一个单例(沙河文件)  用来存储用户名和密码
    NSUserDefaults *user_d = [NSUserDefaults standardUserDefaults];

    [user_d setObject:_nameTF.text forKey:@"name"];
    [user_d setObject:_pswTF.text forKey:@"psw"];

    [user_d synchronize];//同步存储的数据



    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"注册成功" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去登陆", nil];
    alert.tag = 1;
    [alert show];






}
//alertView的按钮点击事件alertView:当前弹框view   buttonIndex:点击的是第几个按钮(从左往右一次是0,1,2...)
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ((buttonIndex == 1) && (alertView.tag == 1)) {//点击去登陆按钮
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值