sqlite

//導航欄sql

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
數據庫

    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];app

    self.window.tintColor = [UIColor redColor];//設置點擊後顏色爲紅色ui

    self.window.backgroundColor = [UIColor whiteColor];atom

    _viewController = [[ViewController alloc]init];spa

    _twoViewController = [[TwoViewController alloc]init];3d

    _threeViewController = [[ThreeViewController alloc]init];orm

    

    

    

    UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:_viewController];sqlite

    UIImage *imageyaowen = [[UIImage imageNamed:@"灰色要聞.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];three

    UIImage *redYaowen = [[UIImage imageNamed:@"紅色要聞.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    navigation.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第一頁" image:imageyaowen selectedImage:redYaowen];

    

    

    

    UINavigationController *sencondnavigation = [[UINavigationController alloc]initWithRootViewController:_twoViewController];

    UIImage *imageweiwenming = [[UIImage imageNamed:@"灰色動態.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    UIImage *redWeiming = [[UIImage imageNamed:@"紅色動態.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    sencondnavigation.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第二頁" image:imageweiwenming selectedImage:redWeiming];

    

    

    

    UINavigationController *thirdnavgation = [[UINavigationController alloc]initWithRootViewController:_threeViewController];

    UIImage *imagegonyi = [[UIImage imageNamed:@"灰色公益.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    UIImage *Redimagegonyi = [[UIImage imageNamed:@"紅色公益.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    thirdnavgation.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第三頁" image:imagegonyi selectedImage:Redimagegonyi];

    

    

    UITabBarController *tabber = [[UITabBarController alloc]init];

    tabber.viewControllers = @[navigation,sencondnavigation,thirdnavgation];

    self.window.rootViewController = tabber;

    return YES;


    return YES;

}



@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationItem.title=@"大熊論壇";

    //設置view的背景顏色

    self.view.backgroundColor=[UIColor redColor];

    

    UIBarButtonItem * button=[[UIBarButtonItem alloc]initWithTitle:@"下一頁" style:UIBarButtonItemStyleDone target:self action:@selector(xiayiye)];

    

    self.navigationItem.rightBarButtonItem=button;

   

}

-(void)xiayiye{

    

    ViewController2 * view=[[ViewController2 alloc]init];

    

    

    [self.navigationController pushViewController:view animated:NO];

}




//SimaPleForum

#import <Foundation/Foundation.h>

#import "sqlite3.h"

@interface UserModel : NSObject

{

    sqlite3 *link;

    NSString *path;

}

//插入數據

-(void)insertMessageWithAccount:(NSString *)account andPassword:(NSString *)password andNickName:(NSString *)nickName;

//註冊時檢查帳號存在性

-(BOOL)jugdeExist:(NSString *)account;


//登陸操做

-(NSMutableArray *)loginMethod:(NSString *)account andPassword:(NSString *)password;


-(NSString *)findName:(NSString *)userId;

@end



#import <Foundation/Foundation.h>

#import "sqlite3.h"

@interface UserModel : NSObject

{

    sqlite3 *link;

    NSString *path;

}

//插入數據

-(void)insertMessageWithAccount:(NSString *)account andPassword:(NSString *)password andNickName:(NSString *)nickName;

//註冊時檢查帳號存在性

-(BOOL)jugdeExist:(NSString *)account;


//登陸操做

-(NSMutableArray *)loginMethod:(NSString *)account andPassword:(NSString *)password;


-(NSString *)findName:(NSString *)userId;

@end



#import <Foundation/Foundation.h>

#import "sqlite3.h"

@interface Artical : NSObject

{

    sqlite3 *link;

    NSString *path;

}

-(void)insertMessageWithTitle:(NSString *)title andContent:(NSString *)content andId:(NSString *)userId;

- ( NSMutableArray * )showListMethod;


#import "Artical.h"


@implementation Artical

-(id)init{

    self=[super init];

    //定義數據庫設置

    path=@"/Users/feifanchengxuyuan/Desktop/Forum.db";

    //打開數據庫連接

    sqlite3_open([path UTF8String], &link);

    //建立用戶表的SQL語句

    NSString *createArticalTable=@"create table if not exists Artical(id integer primary key autoincrement,userId integer,title varchar(20),content varchar(1000),dateTime time)";

//    //建立用戶表的SQL語句自動插入時間

//    NSString *createArticalTable=@"create table if not exists Artical(id integer primary key autoincrement,userId integer,title varchar(20),content varchar(1000),dateTime defalut(datetime('now','localtime')))";

    //執行SQL語句

    sqlite3_exec(link, [createArticalTable UTF8String],nil,nil,nil);

    

    return self;

}

//插入方法

-(void)insertMessageWithTitle:(NSString *)title andContent:(NSString *)content andId:(NSString *)userId{

    NSString * insertMessage=[NSString stringWithFormat:@"insert into Artical(userId,title,content,dateTime) values(\"%@\",\"%@\",\"%@\",datetime(\"now\",\"localTime\"));",userId,title,content];

    

    sqlite3_exec(link, [insertMessage UTF8String] , nil,nil,nil);

}


- ( NSMutableArray * )showListMethod{

    

    sqlite3_stmt *stmt;

    

    NSMutableArray *mutableArray=[[NSMutableArray alloc]initWithCapacity:10];

    

    NSDictionary *dict;

    

    NSString *select=@"select * from Artical";

    

    sqlite3_exec(link, [select UTF8String], nil, nil, nil);

    

    sqlite3_prepare_v2(link, [select UTF8String], -1, &stmt, nil);

    

    while (sqlite3_step(stmt)==SQLITE_ROW) {

        NSString * articalId=[[NSString alloc]initWithCString:(char *)sqlite3_column_text(stmt, 0) encoding:NSUTF8StringEncoding];

        NSString * userId=[[NSString alloc]initWithCString:(char *)sqlite3_column_text(stmt, 1) encoding:NSUTF8StringEncoding];

        NSString *title=[[NSString alloc]initWithCString:(char *)sqlite3_column_text(stmt, 2) encoding:NSUTF8StringEncoding];

        NSString *content=[[NSString alloc]initWithCString:(char *)sqlite3_column_text(stmt, 3) encoding:NSUTF8StringEncoding];

        NSString *time=[[NSString alloc]initWithCString:(char *)sqlite3_column_text(stmt, 4) encoding:NSUTF8StringEncoding];

        dict=@{

               @"articalId":articalId,

               @"userId":userId,

               @"title":title,

               @"content":content,

               @"time":time

               };

        [mutableArray addObject:dict];

    }

    return mutableArray;

}




////

//-(NSMutableArray *)showMethodWith:(NSInteger)userId{

//    sqlite3_stmt *stmt;

//   

//    NSMutableArray *array=[[NSMutableArray alloc]initWithCapacity:10];

//    NSString * selectAccount=[NSString stringWithFormat:@"select * from Artical where id=%li;",userId];

//    sqlite3_prepare_v2(link, [selectAccount UTF8String], -1, &stmt, nil);

//    while (sqlite3_step(stmt)==SQLITE_ROW) {

//        NSString *passwordFromDataBase=[[NSString alloc]initWithCString:(char *)sqlite3_column_text(stmt, 2) encoding:NSUTF8StringEncoding];

//    }

//    

//    return array;

//}




#import <UIKit/UIKit.h>


@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *listTableView;

@property (weak, nonatomic) IBOutlet UILabel *messageLabel;

@property (weak, nonatomic) IBOutlet UIButton *writeButton;

@property (weak, nonatomic) IBOutlet UIButton *registerButton;

@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@property (weak, nonatomic) IBOutlet UIButton *quitButton;


- (IBAction)quitMethod:(id)sender;


@end



#import "ViewController.h"

#import "Artical.h"

#import "UserModel.h"

@interface ViewController ()

@property(nonatomic,strong)NSUserDefaults *userDefault;

@property(nonatomic,strong)NSMutableArray *arrayContent;

@end


@implementation ViewController


-(void)viewDidAppear:(BOOL)animated{

     NSLog(@"-----viewDidAppear-----");

    _userDefault=[NSUserDefaults standardUserDefaults];

    NSString *userName=[_userDefault valueForKey:@"name"];

    NSLog(@"登陸名:%@",userName);

    if (userName == nil) {

        _messageLabel.text=@"你好,遊客。發帖請登陸。";

        _loginButton.hidden=NO;

        _registerButton.hidden=NO;

        _writeButton.hidden=YES;

        _quitButton.hidden=YES;

    }else{

        _messageLabel.text=[NSString stringWithFormat:@"用戶:%@,歡迎回來",userName];

        _loginButton.hidden=YES;

        _registerButton.hidden=YES;

        _writeButton.hidden=NO;

        _quitButton.hidden=NO;

        

    }

    

    Artical *artical=[[Artical alloc]init];

   NSLog(@"-----%@---",[artical showListMethod]);

    _arrayContent=[artical showListMethod];

    

    [_listTableView reloadData];

    

}


- (void)viewDidLoad {

    [super viewDidLoad];

     NSLog(@"-----viewDidLoad-----");

    _arrayContent=[NSMutableArray array];

    

    Artical *artical=[[Artical alloc]init];

    NSLog(@"-----%@---",[artical showListMethod]);

    _arrayContent=[artical showListMethod];

    

    _listTableView.delegate=self;

    _listTableView.dataSource=self;

    

    

    

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [_arrayContent count];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Cell"];

    if (cell==nil) {

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];

    }

    NSLog(@"88888====%@",_arrayContent);

    cell.textLabel.text=_arrayContent[indexPath.row][@"title"];

    UserModel *userModel=[[UserModel alloc]init];

    NSString *userName=[userModel findName:_arrayContent[indexPath.row][@"userId"]];

    NSString *detailText=[NSString stringWithFormat:@"%@      %@",_arrayContent[indexPath.row][@"time"],userName];

    cell.detailTextLabel.text=detailText;

    return cell;

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (IBAction)quitMethod:(id)sender {

    NSLog(@"註銷方法");

    

    [_userDefault removeObjectForKey:@"name"];

    _messageLabel.text=@"你好,遊客。發帖請登陸。";

    _loginButton.hidden=NO;

    _registerButton.hidden=NO;

    _writeButton.hidden=YES;

    _quitButton.hidden=YES;


}

@end



#import <UIKit/UIKit.h>


@interface RegisterViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *accountTextFeild;

@property (weak, nonatomic) IBOutlet UITextField *nickNameTextFeild;

@property (weak, nonatomic) IBOutlet UITextField *passWordTextFeild;

@property (weak, nonatomic) IBOutlet UITextField *writeAgainTextFeild;

- (IBAction)back:(id)sender;

- (IBAction)registMethod:(id)sender;



@end



#import "RegisterViewController.h"

#import "UserModel.h"

@interface RegisterViewController ()


@end


@implementation RegisterViewController



- (void)viewDidLoad {

    [super viewDidLoad];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

   

}



- (IBAction)back:(id)sender {

    [self dismissViewControllerAnimated:YES completion:nil];

}


- (IBAction)registMethod:(id)sender {

    UserModel *user=[[UserModel alloc]init];

    NSString * userAccount=_accountTextFeild.text;

    NSString *userPassword=_passWordTextFeild.text;

    NSString *userAgain=_writeAgainTextFeild.text;

    NSString *userNickName=_nickNameTextFeild.text;

    if ([userAccount length]>=6&&[userAccount length]<=16&&[userPassword length]>=6&&[userPassword length]<=16&&[userAgain length]>=6&&[userAgain length]<=16&&[userNickName length]>=6&&[userNickName length]<=16) {

        if ([userPassword isEqualToString:userAgain]) {

                        

            if ([user jugdeExist:userAccount]) {

                NSLog(@"帳號存在");

                

            }else{

                [user insertMessageWithAccount:_accountTextFeild.text andPassword:_passWordTextFeild.text andNickName:_nickNameTextFeild.text];

                [self dismissViewControllerAnimated:NO completion:nil];

                

            }

           

        }

        else{

            NSLog(@"兩次密碼輸入不一致");

        }

        

    }else {

        NSLog(@"信息填寫不正確");

    }

    

    

    

    

    

}



@end




#import <UIKit/UIKit.h>


@interface LoginViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *accountTextFeild;

@property (weak, nonatomic) IBOutlet UITextField *passwordTextFeild;

@property (weak, nonatomic) IBOutlet UILabel *message;


- (IBAction)loginMethod:(UIButton *)sender;

- (IBAction)back:(id)sender;



@end



#import "LoginViewController.h"

#import "UserModel.h"

@interface LoginViewController ()


@end


@implementation LoginViewController


- (void)viewDidLoad {

    [super viewDidLoad];

   

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

   

}


- (IBAction)back:(id)sender {

    [self dismissViewControllerAnimated:NO completion:nil];

}


- (IBAction)loginMethod:(UIButton *)sender {

    UserModel *userModel=[[UserModel alloc]init];

    NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];

    NSMutableArray *array=[[NSMutableArray alloc]initWithCapacity:10];

    array=[userModel loginMethod:_accountTextFeild.text andPassword:_passwordTextFeild.text];

    NSString *name=array[1];

    NSString *userId=array[0];

    if (![name isEqualToString:@""]) {

        [userDefault setValue:userId forKey:@"id"];

        [userDefault setValue:name forKey:@"name"];

        [userDefault synchronize];

         [self performSegueWithIdentifier:@"longinSuccess" sender:nil];

    }else{

       _message.text=@"登陸失敗,請重試";

       

    }

    NSLog(@"%@",[userDefault valueForKey:@"name"]);

    


//        [self dismissViewControllerAnimated:NO completion:nil];

   

}

@end




#import <UIKit/UIKit.h>


@interface WriteViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *titleName;

@property (weak, nonatomic) IBOutlet UITextView *contentTextView;

- (IBAction)writeToDataBase:(id)sender;

- (IBAction)back:(id)sender;


@end



#import "WriteViewController.h"

#import "Artical.h"

@interface WriteViewController ()


@end


@implementation WriteViewController


- (void)viewDidLoad {

    [super viewDidLoad];

   

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    

}

- (IBAction)writeToDataBase:(id)sender {

    NSLog(@"寫貼");

    Artical *artical=[[Artical alloc]init];

    NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];

    NSString *userId=[userDefault valueForKey:@"id"];

    NSLog(@"%@",userId);

    [artical insertMessageWithTitle:_titleName.text andContent:_contentTextView.text andId:userId];

    [self dismissViewControllerAnimated:NO completion:nil];

    

}


- (IBAction)back:(id)sender {

    NSLog(@"返回");

    [self dismissViewControllerAnimated:NO completion:nil];

}

@end

相關文章
相關標籤/搜索