Register : NSObject
{
sqlite3 *link;
NSString *path;
}
-(void)addNewPeople:(NSString *)addRegister andSecret:(NSString *)addSecret;
-(NSMutableArray *) showAccountInformation;
@implementation Register
-(id)init
{
self=[super init];
//确定数据库文件,完成数据库链接
path=@"/Users/feifanchengxuyuan/Desktop/登录注册.db";
sqlite3_open([path UTF8String], &link);
//创建表
NSString *creatTable=@"create table if not exists register(account varchar(40),secret varchar(20))";
sqlite3_exec(link, [creatTable UTF8String], nil, nil, nil);
return self;
}
// 插入数据
-(void)addNewPeople:(NSString *)addRegister andSecret:(NSString *)addSecret
{
NSString *insertData=[NSString stringWithFormat:@"insert into register(account,secret) values (\"%@\",\"%@\")",addRegister,addSecret];
sqlite3_exec(link, [insertData UTF8String], nil, nil, nil );
}
//查找数据库中的信息
-(NSMutableArray *) showAccountInformation
{
NSMutableArray *informationArray=[[NSMutableArray alloc]initWithCapacity:10];
NSDictionary *informationDict=[[NSDictionary alloc]init];
sqlite3_stmt *state;
NSString *account;
NSString *secret;
NSString *selectData=[NSString stringWithFormat:@"select *from register"];
sqlite3_prepare_v2(link, [selectData UTF8String], -1, &state, nil);
while (sqlite3_step(state)==SQLITE_ROW) {
account= [[NSString alloc ]initWithCString:(char *)sqlite3_column_text( state, 0 )encoding:NSUTF8StringEncoding];
secret= [[NSString alloc ]initWithCString:(char *)sqlite3_column_text( state, 1 )encoding:NSUTF8StringEncoding];
informationDict=@{ @"account":account,@"secret":secret};
[informationArray addObject:informationDict];
}
return informationArray;
}
#import "ViewController.h"
#import "Register.h"
@interface ViewController ()
@property(nonatomic,strong)RegisterViewController *registerView;
@property(nonatomic,strong)UITextField *fieldAccount;
@property(nonatomic,strong)UITextField *fieldSecret;
@property(nonatomic,strong)UILabel *label;
-(void)promptMessage:(NSString *)str;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//给背景图片
CGRect rect= [[UIScreen mainScreen]bounds];
UIImageView *image=[[UIImageView alloc]initWithFrame:rect];
image.image=[UIImage imageNamed:@"0"];
[self.view addSubview:image];
self.navigationItem.title=@"登陆界面";
//账户
UILabel *account=[[UILabel alloc]initWithFrame:CGRectMake(80, 150, 38, 28)];
account.text=@"账户";
[self.view addSubview:account];
//密码
UILabel *secret=[[UILabel alloc]initWithFrame:CGRectMake(80, 220, 38, 28)];
secret.text=@"密码";
// secret.backgroundColor=[UIColor greenColor];
[self.view addSubview:secret];
//输入账户
_fieldAccount=[[UITextField alloc]initWithFrame:CGRectMake(140, 150, 170, 28)];
_fieldAccount.placeholder=@" 请输入账号";
_fieldAccount.layer.borderWidth=2;
_fieldAccount.layer.borderColor=[[UIColor alloc]initWithRed:0.2 green:0.4 blue:0.7 alpha:0.2].CGColor;
_fieldAccount.layer.cornerRadius=10;
[self.view addSubview:_fieldAccount];
//输入密码
_fieldSecret=[[UITextField alloc]initWithFrame:CGRectMake(140, 220, 170, 28)];
_fieldSecret.placeholder=@" 请输入密码";
// fieldSecret.backgroundColor=[UIColor greenColor];
_fieldSecret.layer.borderWidth=2;
_fieldSecret.layer.borderColor=[[UIColor alloc]initWithRed:0.2 green:0.4 blue:0.7 alpha:0.2].CGColor;
_fieldSecret.layer.cornerRadius=10;
[self.view addSubview:_fieldSecret];
//提示框
_label=[[UILabel alloc]initWithFrame:CGRectMake(140, 260, 170, 28)];
// _label.backgroundColor=[UIColor redColor];
[self.view addSubview:_label];
//登录按钮
UIButton *butInter=[[UIButton alloc]initWithFrame:CGRectMake(100, 330, 40, 28)];
[butInter setTitle:@"登录" forState:UIControlStateNormal];
[butInter setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// butInter.backgroundColor=[[UIColor alloc]initWithRed:0.0 green:0.2 blue:0.8 alpha:0.3];
[butInter setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
[butInter addTarget:self action:@selector(clickInter) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:butInter];
//注册按钮
UIButton *butRegister=[[UIButton alloc]initWithFrame:CGRectMake(245, 330, 40, 28)];
[butRegister setTitle:@"注册" forState:UIControlStateNormal];
[butRegister setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// butRegister.backgroundColor=[[UIColor alloc]initWithRed:0.0 green:0.2 blue:0.8 alpha:0.3];
[butRegister setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
[self.view addSubview:butRegister];
[butRegister addTarget:self action:@selector(clickRegister) forControlEvents:UIControlEventTouchUpInside];
}
-(void)promptMessage:(NSString *)str
{
_label.text=str ;
_label.textAlignment=NSTextAlignmentCenter;
_label.textColor=[UIColor redColor];
_label.layer.borderWidth=2;
_label.layer.borderColor=[[UIColor alloc]initWithRed:0.2 green:0.4 blue:0.7 alpha:0.2].CGColor;
_label.layer.cornerRadius=10;
}
# pragma 判断账号和密码
-(int)judgeWithText:(NSString *)textAccount andSecret:(NSString*)secret and:(Register *)data
{
NSMutableArray *array=[[NSMutableArray alloc]init];
array=[data showAccountInformation];
int flag=0;
for (int i=0; i<[array count]; i++) {
if ([textAccount isEqual: array[i][@"account"] ]) {
if ([secret isEqual:array[i][@"secret"]]) {
flag++;
}
}
}
return flag;
}
-(void)clickInter
{
Register *data=[[Register alloc]init];
if ([self judgeWithText:_fieldAccount.text andSecret:_fieldSecret.text and:data]!=1) {
[self promptMessage:@"账号或密码错误"];
}
else
{
// _label.text=@"";
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示框" message:@"登录成功" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
}
-(void)clickRegister
{
_registerView=[[RegisterViewController alloc]init];
[self.navigationController pushViewController:_registerView animated:YES];
// [self presentViewController:<#(UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#>]
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#import "RegisterViewController.h"
#import "Register.h"
//#define kAlphaNum @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
@interface RegisterViewController ()
@property(nonatomic,strong)UITextField *fieldAccount;
@property(nonatomic,strong)UITextField *oneSecret;
@property(nonatomic,strong)UITextField *twoSecret;
@property(nonatomic,strong)UILabel *label;
-(void)promptMessage:(UILabel *)label and: (NSString *)message;
-(int)judgeWithText:(NSString *)text and:(Register *)data;
@end
@implementation RegisterViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect rect=[[UIScreen mainScreen]bounds];
UIImageView *image=[[UIImageView alloc]initWithFrame:rect];
image.image=[UIImage imageNamed:@"1"];
[self.view addSubview:image];
self.navigationItem.title=@"注册界面";
//用户名
UILabel *account=[[UILabel alloc]initWithFrame:CGRectMake(80, 150, 60, 28)];
account.text=@"用户名";
[self.view addSubview:account];
//新密码
UILabel *secret=[[UILabel alloc]initWithFrame:CGRectMake(80, 220, 60, 28)];
secret.text=@"新密码";
[self.view addSubview:secret];
//确认密码
UILabel *confirmSecret=[[UILabel alloc]initWithFrame:CGRectMake(68, 290, 80, 28)];
confirmSecret.text=@"确认密码";
[self.view addSubview:confirmSecret];
//输入用户名
_fieldAccount=[[UITextField alloc]initWithFrame:CGRectMake(150, 150, 170, 28)];
_fieldAccount.placeholder=@" 请输入用户名(6-12位)";
_fieldAccount.layer.borderWidth=2;
_fieldAccount.layer.borderColor=[[UIColor alloc]initWithRed:0.2 green:0.4 blue:0.7 alpha:0.2].CGColor;
_fieldAccount.layer.cornerRadius=10;
[self.view addSubview:_fieldAccount];
//输入新密码
_oneSecret=[[UITextField alloc]initWithFrame:CGRectMake(150, 220, 170, 28)];
_oneSecret.placeholder=@" 请输入新密码";
_oneSecret.layer.borderWidth=2;
_oneSecret.layer.borderColor=[[UIColor alloc]initWithRed:0.2 green:0.4 blue:0.7 alpha:0.2].CGColor;
_oneSecret.layer.cornerRadius=10;
[self.view addSubview:_oneSecret];
//确认密码
_twoSecret=[[UITextField alloc]initWithFrame:CGRectMake(150, 290, 170, 28)];
_twoSecret.placeholder=@" 请确认密码";
_twoSecret.layer.borderWidth=2;
_twoSecret.layer.borderColor=[[UIColor alloc]initWithRed:0.2 green:0.4 blue:0.7 alpha:0.2].CGColor;
_twoSecret.layer.cornerRadius=10;
[self.view addSubview:_twoSecret];
//提示标签
_label=[[UILabel alloc]initWithFrame:CGRectMake(150, 325, 170, 28)];
// _twoLabel.backgroundColor=[UIColor blackColor];
[self.view addSubview:_label];
//注册按钮
UIButton *butRegister=[[UIButton alloc]initWithFrame:CGRectMake(150, 400, 100, 28)];
[butRegister setTitle:@"注册" forState:UIControlStateNormal];
[butRegister setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// butRegister.backgroundColor=[[UIColor alloc]initWithRed:0.0 green:0.2 blue:0.8 alpha:0.3];
[butRegister addTarget:self action:@selector(clickRegister) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:butRegister];
}
# pragma 提示信息的设置
-(void)promptMessage:(UILabel *)label and: (NSString *)message
{
label.text=message;
label.textAlignment=NSTextAlignmentCenter;
label.textColor=[UIColor redColor];
label.layer.borderWidth=2;
label.layer.borderColor=[[UIColor alloc]initWithRed:0.2 green:0.4 blue:0.7 alpha:0.2].CGColor;
label.layer.cornerRadius=10;
}
# pragma 判断账号是否重复
-(int)judgeWithText:(NSString *)text and:(Register *)data
{
NSMutableArray *array=[[NSMutableArray alloc]init];
array=[data showAccountInformation];
int flag=0;
for (int i=0; i<[array count]; i++) {
if ([text isEqual: array[i][@"account"] ]) {
flag++;
}
}
return flag;
}
#pragma 注册账号
-(void)clickRegister
{
Register *addData=[[Register alloc]init];
//判断信息是否为空
if (([_fieldAccount.text isEqual:@""])||( [_oneSecret.text isEqual:@""]) || ([_twoSecret.text isEqual:@""])) {
[self promptMessage:_label and:@"个人信息不能为空!"];
}
//判断用户名长度是否超限制
else if ((_fieldAccount.text.length>12)||(_fieldAccount.text.length<6))
{
[self promptMessage:_label and:@"请重新输入用户名"];
}
//确认密码
else if (![_oneSecret.text isEqual: _twoSecret.text])
{
[self promptMessage:_label and:@"请重新确认密码"];
}
else if ([self judgeWithText:_fieldAccount.text and:addData]!=0)
{
[self promptMessage:_label and:@"此账号已存在"];
}
else
{
//将数据添加进数据库
[addData addNewPeople:_fieldAccount.text andSecret:_oneSecret.text];
//注册成功的时候给一个提示框,告诉用户注册成功
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示框" message:@"注册成功" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
//注册成功自动跳转到登录界面
[self.navigationController popToRootViewControllerAnimated:YES];
}