轉(zhuǎn)載別人的文章
//聯(lián)系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪哄
/**
注意點: 1.看 GIF 效果圖.
2.看連線視圖的效果圖.
3.看實現(xiàn)代碼(直接復(fù)制實現(xiàn)效果).
4.看實現(xiàn)代碼MVC架構(gòu)
*/
一、GIF 效果圖:
二、連線視圖的效果圖:
圖1:
圖2:
圖3:
三、實現(xiàn)代碼:
=======
================================
=======
1.AppDelegate.m 中的代碼
#import"AppDelegate.h"
#import"ViewController.h"http://主控制器
@interfaceAppDelegate()
@end
@implementationAppDelegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
//初始化
ViewController*vc = [[ViewControlleralloc]init];
//導(dǎo)航控制器
UINavigationController*nvc = [[UINavigationControlleralloc]initWithRootViewController:vc];
//添加圖
self.window.rootViewController= nvc;
returnYES;
}
================================
=======
2.導(dǎo)入第三方數(shù)據(jù)庫 FMDB 框架包
下載地址:https://github.com/ccgus/fmdb
注意:要導(dǎo)入 libsqlite3.tbd 包 (詳情請看上面圖3)
================================
=======
3.在 Other 模塊中添加業(yè)務(wù)處理層處理 SQLite 語句
注意:如果現(xiàn)在拷貝到項目會報錯-->原因是其他 MVC 模塊中的類沒有實現(xiàn)
控制器1
.h:
//? Created by石虎on 2017/7/6.
//? Copyright ? 2017年shihu. All rights reserved.
//
#import
#import"Model.h"
#import"FMDatabase.h"
@interfaceLoadData :NSObject
//單列類
+(instancetype)sharlLoadData;
//添加元素
-(void)AddsharlLoadData:(Model*)model;
//查詢
-(NSMutableArray*)Marr;
//刪除元素
-(void)deleteharlLoadData:(Model*)model;
//修改元素
-(void)UPsharlLoadData:(Model*)model;
@end
控制器1
.M:
//? Created by石虎on 2017/7/6.
//? Copyright ? 2017年shihu. All rights reserved.
//
#import"LoadData.h"
staticLoadData*ld =nil;
staticFMDatabase*fate;
@implementationLoadData
//單列類
+(instancetype)sharlLoadData{
//靜態(tài)
staticdispatch_once_toneet;
//初始化
dispatch_once(&oneet, ^{
ld= [[LoadDataalloc]init];
//定義初始化
[ldinitA];
});
//返回值
returnld;
}
//初始化
+(instancetype)allocWithZone:(struct_NSZone*)zone{
if(!ld) {
//初始化
ld= [superallocWithZone:zone];
}
returnld;
}
//淺復(fù)制
-(id)copy{
returnself;
}
//深復(fù)制
-(id)mutableCopy{
returnself;
}
//初始化數(shù)據(jù)庫
-(void)initA{
//創(chuàng)建沙盒
NSString*Ste = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,YES)objectAtIndex:0];
//定義文件名
NSString*path = [StestringByAppendingPathComponent:@"HousingInfo.sqlite"];
//初始化
fate= [[FMDatabasealloc]initWithPath:path];
//判斷
if([fateopen]) {
//初始化
[fateexecuteUpdate:@"create table class (ID integer primary
key, fieldMail text, fieldAddress text , fieldConsignee text , fieldPhone text , fieldNote text)"];
[fateclose];
NSLog(@"成功");
}else{
NSLog(@"失敗");
}
}
//添加元素
-(void)AddsharlLoadData:(Model*)model{
//開始
[fateopen];
//初始化
NSString*str = [NSStringstringWithFormat:@"insert
into class values (null , '%@','%@','%@','%@','%@')",model.fieldMail,model.fieldAddress,
model.fieldConsignee,model.fieldPhone,model.fieldNote];
//BOOL值接受
BOOLii = [fateexecuteUpdate:str];
//判斷
if(ii) {
NSLog(@"成功");
}else{
NSLog(@"失敗");
}
//關(guān)閉
[fateclose];
}
//查詢
-(NSMutableArray*)Marr{
//初始化
NSMutableArray*marr = [NSMutableArraynew];
//開始
[fateopen];
//初始化
FMResultSet*Set = [[FMResultSetalloc]init];
//使用set接受
Set = [fateexecuteQuery:@"select * from class"];
//判斷
while([Setnext]) {
//初始化
Model*mm = [Modelnew];
//鏈接
mm.fieldMail= [SetstringForColumn:@"fieldMail"];
mm.fieldAddress = [Set stringForColumn:@"fieldAddress"];
mm.fieldConsignee = [Set stringForColumn:@"fieldConsignee"];
mm.fieldPhone = [Set stringForColumn:@"fieldPhone"];
mm.fieldNote = [Set stringForColumn:@"fieldNote"];
mm.ID = [Set intForColumn:@"ID"];
//添加到數(shù)組
[marr addObject:mm];
}
//關(guān)閉
[fate close];
//返回值
returnmarr;
}
//刪除元素
-(void)deleteharlLoadData:(Model *)model{
//開始
[fate open];
//初始化
NSString *str = [NSString stringWithFormat:@"delete from class where ID = '%ld' ",model.ID];
//BOOL值接受
BOOLii = [fate executeUpdate:str];
//判斷
if(ii) {
NSLog(@"成功");
}else{
NSLog(@"失敗");
}
//關(guān)閉
[fate close];
}
//修改元素
-(void)UPsharlLoadData:(Model *)model{
//開始
[fate open];
//初始化
NSString *str = [NSString stringWithFormat:@"update
class set fieldMail = '%@',fieldAddress = '%@',fieldConsignee =
'%@',fieldPhone = '%@',fieldNote = '%@' where ID = '%ld'",model.fieldMail,model.fieldAddress,model.fieldConsignee,
model.fieldPhone,model.fieldNote,model.ID]
//BOOL值接受
BOOLii = [fate executeUpdate:str];
//判斷
if(ii) {
NSLog(@"成功");
}else{
NSLog(@"失敗");
}
//關(guān)閉
[fate close];
}
@end
================ ? ? ================
=======
4.MVC-->C模塊
控制器1 -主控制器 ?.m:
//? Created by石虎on 2017/7/6.
//? Copyright ? 2017年shihu. All rights reserved.
//
#import"ViewController.h"
#import"LoadData.h"http://業(yè)務(wù)處理SQLite
#import"Model.h"http://保存數(shù)據(jù)
#import"MyTableViewCell.h"http://主控制器cell
#import"UpViewController.h"http://修改信息控制器
#import"AddViewController.h"http://添加信息控制器
@interfaceViewController(){
UITableView*tabele;
NSMutableArray*marr;
}
@end
@implementationViewController
//將要顯示
-(void)viewWillAppear:(BOOL)animated{
//查詢
marr= [[LoadDatasharlLoadData]Marr];
//刷新
[tabelereloadData];
}
- (void)viewDidLoad {
[superviewDidLoad];
//定義標題
self.title=@"信息查詢系統(tǒng)";
//初始化
tabele= [[UITableViewalloc]initWithFrame:self.view.framestyle:
UITableViewStylePlain];
//添加協(xié)議
tabele.delegate=self;
tabele.dataSource=self;
//添加到試圖上
[self.viewaddSubview:tabele];
//定義按鈕
UIBarButtonItem*right = [[UIBarButtonItemalloc]
initWithTitle:@"添加"style:UIBarButtonItemStylePlain
target:selfaction:@selector(click)];
//添加到導(dǎo)航調(diào)試
self.navigationItem.rightBarButtonItem=
right;
}
-(void)click{
//初始化
AddViewController*add = [AddViewControllernew];
//跳轉(zhuǎn)
[self.navigationControllerpushViewController:addanimated:YES];
}
//行數(shù)
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
returnmarr.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
//初始化
MyTableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:@"CELL"];
//復(fù)用池
if(!cell) {
//初始化‘
cell = [[MyTableViewCellalloc]initWithStyle:
UITableViewCellStyleSubtitlereuseIdentifier:@"CELL"];
}
//初始化
Model*mm =marr[indexPath.row];
//添加到表格上
cell.fieldMail.text= mm.fieldMail;
cell.fieldAddress.text= mm.fieldAddress;
cell.fieldConsignee.text= mm.fieldConsignee;
cell.fieldPhone.text= mm.fieldPhone;
cell.fieldNote.text= mm.fieldNote;
//返回值
returncell;
}
//刪除
-(void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath*)indexPath{
//添加
Model*mm? =marr[ indexPath.row];
//刪除
[[LoadDatasharlLoadData]deleteharlLoadData:mm];
[marr removeObjectAtIndex:indexPath.row];
//刷新
[tabele reloadData];
}
//跳轉(zhuǎn)
-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//初始化
UpViewController *up = [UpViewController new];
//添加
up.mm = marr[indexPath.row];
//修改
[[LoadData sharlLoadData]UPsharlLoadData:up.mm];
//跳轉(zhuǎn)
[self.navigationController pushViewController:up animated:YES];
}
@end
================================
控制器2--添加信息控制器 .m:
//? Created by石虎on 2017/7/6.
//? Copyright ? 2017年shihu. All rights reserved.
//
#import"AddViewController.h"
#import"Model.h"
#import"LoadData.h"
@interfaceAddViewController()
//郵件
@property(strong,nonatomic)IBOutletUITextField*fieldMail;
//收貨地址
@property(strong,nonatomic)IBOutletUITextField*fieldAddress;
//收貨人
@property(strong,nonatomic)IBOutletUITextField*fieldConsignee;
//電話
@property(strong,nonatomic)IBOutletUITextField*fieldPhone;
//備注
@property(strong,nonatomic)IBOutletUITextField*fieldNote;
//點擊添加信息
- (IBAction)sss:(id)sender;
@end
@implementationAddViewController
- (void)viewDidLoad {
[superviewDidLoad];
}
- (IBAction)sss:(id)sender {
//初始化
Model*mm = [Modelnew];
//鏈接
mm.fieldMail=self.fieldMail.text;
mm.fieldAddress=self.fieldAddress.text;
mm.fieldConsignee=self.fieldConsignee.text;
mm.fieldPhone=self.fieldPhone.text;
mm.fieldNote=self.fieldNote.text;
//添加到數(shù)據(jù)庫
[[LoadDatasharlLoadData]AddsharlLoadData:mm];
//跳轉(zhuǎn)
[self.navigationControllerpopViewControllerAnimated:YES];
}
@end
================================
控制器3--修改信息控制器 .h:
//? Created by石虎on 2017/7/6.
//? Copyright ? 2017年shihu. All rights reserved.
//
#import
@classModel;
@interfaceUpViewController :UIViewController
//定義屬性
@property(nonatomic,strong)Model*mm;
@end
控制器3--修改信息控制器 .m:
//? Created by石虎on 2017/7/6.
//? Copyright ? 2017年shihu. All rights reserved.
//
#import"UpViewController.h"
#import"LoadData.h"
#import"Model.h"
@interfaceUpViewController()
//郵件
@property(strong,nonatomic)IBOutletUITextField*fieldMail;
//收貨地址
@property(strong,nonatomic)IBOutletUITextField*fieldAddress;
//收貨人
@property(strong,nonatomic)IBOutletUITextField*fieldConsignee;
//電話
@property(strong,nonatomic)IBOutletUITextField*fieldPhone;
//備注
@property(strong,nonatomic)IBOutletUITextField*fieldNote;
//點擊修改信息
- (IBAction)ssss:(id)sender;
@end
@implementationUpViewController
- (void)viewDidLoad {
[superviewDidLoad];
//將數(shù)據(jù)添加到修改頁面
self.fieldMail.text =self.mm.fieldMail;
self.fieldAddress.text =self.mm.fieldAddress;
self.fieldConsignee.text =self.mm.fieldConsignee;
self.fieldPhone.text =self.mm.fieldPhone;
self.fieldNote.text =self.mm.fieldNote;
}
- (IBAction)ssss:(id)sender {
//初始化
Model *mm =self.mm;
//鏈接
mm.fieldMail =self.fieldMail.text;
mm.fieldAddress =self.fieldAddress.text;
mm.fieldConsignee =self.fieldConsignee.text;
mm.fieldPhone =self.fieldPhone.text;
mm.fieldNote =self.fieldNote.text;
//添加
[[LoadData sharlLoadData]UPsharlLoadData:mm];
//跳轉(zhuǎn)
[self.navigationController popViewControllerAnimated:YES];
}
@end
================================
=======
5.MVC-->V模塊
控制器1-- .h:
//? Created by石虎on 2017/7/6.
//? Copyright ? 2017年shihu. All rights reserved.
//
#import
@interfaceMyTableViewCell :UITableViewCell
//郵件
@property(nonatomic,strong)UILabel*fieldMail;
//收貨地址
@property(nonatomic,strong)UILabel*fieldAddress;
//收貨人
@property(nonatomic,strong)UILabel*fieldConsignee;
//電話
@property(nonatomic,strong)UILabel*fieldPhone;
//備注
@property(nonatomic,strong)UILabel*fieldNote;
@end
控制器1-- .m:
//? Created by石虎on 2017/7/6.
//? Copyright ? 2017年shihu. All rights reserved.
//
#import"MyTableViewCell.h"
@implementationMyTableViewCell
//重寫父類方法
-(instancetype)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString*)reuseIdentifier{
//判斷
if([superinitWithStyle:stylereuseIdentifier:reuseIdentifier])
{
//添加到
[self.contentViewaddSubview:self.fieldMail];
[self.contentViewaddSubview:self.fieldAddress];
[self.contentViewaddSubview:self.fieldConsignee];
[self.contentViewaddSubview:self.fieldPhone];
[self.contentViewaddSubview:self.fieldNote];
}
//返回值
returnself;
}
//懶加載-//郵件
-(UILabel*)fieldMail{
//判斷
if(!_fieldMail) {
//初始化
_fieldMail= [[UILabelalloc]initWithFrame:CGRectMake(5,5,80,44)];
}
//返回值
return_fieldMail;
}
//懶加載-//收貨地址
-(UILabel*)fieldAddress{
//判斷
if(!_fieldAddress) {
//初始化
_fieldAddress= [[UILabelalloc]initWithFrame:CGRectMake(80,5,80,44)];
}
//返回值
return_fieldAddress;
}
//懶加載=//收貨人
-(UILabel*)fieldConsignee{
//判斷
if(!_fieldConsignee) {
//初始化
_fieldConsignee= [[UILabelalloc]initWithFrame:CGRectMake(155,5,80,44)];
}
//返回值
return_fieldConsignee;
}
//懶加載--//電話
-(UILabel*)fieldPhone{
//判斷
if(!_fieldPhone) {
//初始化
_fieldPhone = [[UILabel alloc]initWithFrame:CGRectMake(220,5,80,44)];
}
//返回值
return_fieldPhone;
}
//懶加載
-(UILabel *)fieldNote{
//判斷
if(!_fieldNote) {
//初始化
_fieldNote = [[UILabel alloc]initWithFrame:CGRectMake(305,5,80,44)];
}
//返回值
return_fieldNote;
}
- (void)awakeFromNib {
[superawakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[supersetSelected:selected animated:animated];
}
@end
================================
=======
6.MVC-->M模塊
控制器1-- .h:
//? Created by石虎on 2017/7/6.
//? Copyright ? 2017年shihu. All rights reserved.
//
#import
@interfaceModel :NSObject
//定義屬性
//郵箱
@property(nonatomic,copy)NSString*fieldMail;
//收貨地址
@property(nonatomic,copy)NSString*fieldAddress;
//收貨人
@property(nonatomic,copy)NSString*fieldConsignee;
//電話
@property(nonatomic,copy)NSString*fieldPhone;
//備注
@property(nonatomic,copy)NSString*fieldNote;
//
@property(nonatomic,assign)NSIntegerID;
@end
================================
控制器1--
.m:
//? Created by石虎on 2017/7/6.
//? Copyright ? 2017年shihu. All rights reserved.
//
#import"Model.h"
@implementationModel
@end
================================
=======
================================
=======