//
// ViewController.m
// 登陆和注册
//
// Created by Congyz on 15/8/11.
// Copyright (c) 2015年 Congyz. All rights reserved.
//
#import "ViewController.h"
#import <sqlite3.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
isRember=NO;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)remTap:(UIButton *)sender {
NSLog(@"remTap");
if (isRember==NO)//未记住
{
// 背景改为记住股票
[sender setBackgroundImage:[UIImage imageNamed:@"favorite.png"] forState:UIControlStateNormal];
isRember=YES;//修改表示为记住
}
else
{
//背景切换为不记住图片
[sender setBackgroundImage:[UIImage imageNamed:@"pin.png"] forState:UIControlStateNormal];
isRember=NO;//修改为不记住
}
}
- (IBAction)loginTap:(UIButton *)sender {
NSLog(@"lgoin");
}
- (IBAction)regTap:(UIButton *)sender {
NSLog(@"reg");
//得到要注册的用户名和密码
NSString * uname=self.unametext.text;
NSString * upass=self.upasstext.text;
//剔除空格
uname=[uname stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
upass=[upass stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"name=%@,pass=%@",uname,upass);
//数据有效性验证
if ([uname isEqualToString:@""])//账号为空
{
//创建警报
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"tips" message:@"用户名不能为空" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
//显示警报
[alert show];
//清空内容
self.unametext.text=@"";
//获得焦点
[self.unametext becomeFirstResponder];
return;
}
if ([upass isEqualToString:@""])//密码为空
{
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"tips" message:@"密码不能为空" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
self.upasstext.text=@"";
[self.upasstext becomeFirstResponder];
return;
}
//关键盘
[self closeKB:nil];
//改变注册按钮显示文本
[self.regButton setTitle:@"正在注册..." forState:UIControlStateNormal];
//封锁注册按钮
//self.regButton.enabled=NO;
//封锁整个试图
self.view.userInteractionEnabled=NO;
//检查用户是否存在于(数据库)
//如果用户存在就提示用户更换用户名
//如果不存在就创建用户(数据库)
//打开数据库
//创建要打开数据库的路径和文件名
NSString * path=NSHomeDirectory();
path=[path stringByAppendingPathComponent:@"user.sqlite"];
NSLog(@"%@",path);
sqlite3 * db;
//打开数据库
int result = sqlite3_open([path UTF8String], &db);
if (result!=SQLITE_OK)//打开不成功
{
//创建警报
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"错误" message:@"打开数据库失败" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
//显示警报
[alert show];
return;
}
//在这个数据库中创建用户信息表
char * sql="create table if not exists t_user(uname text not null,upass text note null)";
char * error;
//运行sql
result=sqlite3_exec(db, sql, NULL, NULL, &error);
if (result!=SQLITE_OK)//如果运行失败
{
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"ERROR" message:@"创建数据库失败" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
//显示警报
[alert show];
//关闭数据库
sqlite3_close(db);
return;
}
//关闭数据库
sqlite3_close(db);
}
- (IBAction)closekeyboard:(UITextField *)sender {
}
- (IBAction)closeKB:(UIControl *)sender {
//失去焦点
[self.unametext resignFirstResponder];
[self.upasstext resignFirstResponder];
}
@end
// ViewController.m
// 登陆和注册
//
// Created by Congyz on 15/8/11.
// Copyright (c) 2015年 Congyz. All rights reserved.
//
#import "ViewController.h"
#import <sqlite3.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
isRember=NO;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)remTap:(UIButton *)sender {
NSLog(@"remTap");
if (isRember==NO)//未记住
{
// 背景改为记住股票
[sender setBackgroundImage:[UIImage imageNamed:@"favorite.png"] forState:UIControlStateNormal];
isRember=YES;//修改表示为记住
}
else
{
//背景切换为不记住图片
[sender setBackgroundImage:[UIImage imageNamed:@"pin.png"] forState:UIControlStateNormal];
isRember=NO;//修改为不记住
}
}
- (IBAction)loginTap:(UIButton *)sender {
NSLog(@"lgoin");
}
- (IBAction)regTap:(UIButton *)sender {
NSLog(@"reg");
//得到要注册的用户名和密码
NSString * uname=self.unametext.text;
NSString * upass=self.upasstext.text;
//剔除空格
uname=[uname stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
upass=[upass stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"name=%@,pass=%@",uname,upass);
//数据有效性验证
if ([uname isEqualToString:@""])//账号为空
{
//创建警报
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"tips" message:@"用户名不能为空" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
//显示警报
[alert show];
//清空内容
self.unametext.text=@"";
//获得焦点
[self.unametext becomeFirstResponder];
return;
}
if ([upass isEqualToString:@""])//密码为空
{
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"tips" message:@"密码不能为空" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
self.upasstext.text=@"";
[self.upasstext becomeFirstResponder];
return;
}
//关键盘
[self closeKB:nil];
//改变注册按钮显示文本
[self.regButton setTitle:@"正在注册..." forState:UIControlStateNormal];
//封锁注册按钮
//self.regButton.enabled=NO;
//封锁整个试图
self.view.userInteractionEnabled=NO;
//检查用户是否存在于(数据库)
//如果用户存在就提示用户更换用户名
//如果不存在就创建用户(数据库)
//打开数据库
//创建要打开数据库的路径和文件名
NSString * path=NSHomeDirectory();
path=[path stringByAppendingPathComponent:@"user.sqlite"];
NSLog(@"%@",path);
sqlite3 * db;
//打开数据库
int result = sqlite3_open([path UTF8String], &db);
if (result!=SQLITE_OK)//打开不成功
{
//创建警报
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"错误" message:@"打开数据库失败" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
//显示警报
[alert show];
return;
}
//在这个数据库中创建用户信息表
char * sql="create table if not exists t_user(uname text not null,upass text note null)";
char * error;
//运行sql
result=sqlite3_exec(db, sql, NULL, NULL, &error);
if (result!=SQLITE_OK)//如果运行失败
{
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"ERROR" message:@"创建数据库失败" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
//显示警报
[alert show];
//关闭数据库
sqlite3_close(db);
return;
}
//关闭数据库
sqlite3_close(db);
}
- (IBAction)closekeyboard:(UITextField *)sender {
}
- (IBAction)closeKB:(UIControl *)sender {
//失去焦点
[self.unametext resignFirstResponder];
[self.upasstext resignFirstResponder];
}
@end