1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#import "AppDelegate.h"
#import "StartViewController.h"
#import "SQLViewController.h"
#import "MessageInfoController.h"
@
implementation
AppDelegate
@
synthesize
window
=
_window
;
-
(
void
)
dealloc
{
[
_window
release
]
;
[
super
dealloc
]
;
}
-
(
BOOL
)
application
:
(
UIApplication
*
)
application
didFinishLaunchingWithOptions
:
(
NSDictionary
*
)
launchOptions
{
TTNavigator
*
navigator
=
[
TTNavigator
navigator
]
;
navigator
.
persistenceMode
=
TTNavigatorPersistenceModeAll
;
navigator
.
window
=
[
[
[
UIWindow
alloc
]
initWithFrame
:
TTScreenBounds
(
)
]
autorelease
]
;
TTURLMap
*
map
=
navigator
.
URLMap
;
[
map
from
:
@"*"
toViewController
:
[
TTWebController
class
]
]
;
[
map
from
:
@"tt://StartView"
toSharedViewController
:
[
StartViewController
class
]
]
;
[
map
from
:
@"tt://SQLView/(initInfo:)"
toViewController
:
[
SQLViewController
class
]
]
;
[
map
from
:
@"tt://MessageView/(initInfo:)"
toViewController
:
[
MessageInfoController
class
]
]
;
if
(
!
[
navigator
restoreViewControllers
]
)
{
[
navigator
openURLAction
:
[
TTURLAction
actionWithURLPath
:
@"tt://StartView"
]
]
;
}
return
YES
;
}
@
end
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
#import "StartViewController.h"
@
implementation
StartViewController
-
(
void
)
viewDidLoad
{
[
super
viewDidLoad
]
;
//标题栏名称
self
.
title
=
@"雨松MOMO"
;
//设置视图背景颜色
self
.
view
.
backgroundColor
=
[
UIColor
blackColor
]
;
//创建图片视图
TTImageView
*
imageview
=
[
[
[
TTImageView
alloc
]
initWithFrame
:
CGRectMake
(
100
,
50
,
120
,
120
)
]
autorelease
]
;
//设置图片视图显示的图片资源
imageview
.
defaultImage
=
TTIMAGE
(
@"bundle://0.jpg"
)
;
//将图片视图加入整个视图当中
[
self
.
view
addSubview
:
imageview
]
;
//创建label视图
UILabel
*
label
=
[
[
UILabel
alloc
]
initWithFrame
:
CGRectMake
(
0
,
0
,
320
,
30
)
]
;
//设置显示内容
label
.
text
=
@"Three20软件开发之FMDB数据库"
;
//设置背景颜色
label
.
backgroundColor
=
[
UIColor
blueColor
]
;
//设置文字颜色
label
.
textColor
=
[
UIColor
whiteColor
]
;
//设置显示位置居中
label
.
textAlignment
=
UITextAlignmentCenter
;
//设置字体大小
label
.
font
=
[
UIFont
fontWithName
:
[
[
UIFont
familyNames
]
objectAtIndex
:
10
]
size
:
20
]
;
[
self
.
view
addSubview
:
label
]
;
NSArray
*
titles
=
[
[
NSArray
alloc
]
initWithObjects
:
@"插入数据库"
,
@"更新数据库"
,
@"删除数据库"
,
@"查询数据库"
,
nil
]
;
for
(
int
i
=
0
;
i
<
[
titles
count
]
;
i
++
)
{
//创建普通按钮
UIButton
*
button
=
[
UIButton
buttonWithType
:
1
]
;
//设置按钮位置
button
.
frame
=
CGRectMake
(
0
,
200
+
i
*
40
,
320
,
30
)
;
//设置按钮现实文字
[
button
setTitle
:
[
titles
objectAtIndex
:
i
]
forState
:
UIControlStateNormal
]
;
//设置按钮标记
button
.
tag
=
i
;
//设置按钮点击后 绑定响应方法
[
button
addTarget
:
self
action
:
@
selector
(
ButtonPressed
:
)
forControlEvents
:
UIControlEventTouchUpInside
]
;
//将按钮添加入视图中
[
self
.
view
addSubview
:
button
]
;
}
}
//按钮点击后将进入这个方法
-
(
void
)
ButtonPressed
:
(
id
)
buttonID
{
//得到导航对象
TTNavigator
*
navigator
=
[
TTNavigator
navigator
]
;
//获取点击的按钮
UIButton
*
button
=
(
UIButton
*
)
buttonID
;
//注解1
switch
(
button
.
tag
)
{
case
0
:
[
navigator
openURLAction
:
[
[
TTURLAction
actionWithURLPath
:
@"tt://SQLView/0"
]
applyAnimated
:
YES
]
]
;
break
;
case
1
:
[
navigator
openURLAction
:
[
[
TTURLAction
actionWithURLPath
:
@"tt://SQLView/1"
]
applyAnimated
:
YES
]
]
;
break
;
case
2
:
[
navigator
openURLAction
:
[
[
TTURLAction
actionWithURLPath
:
@"tt://SQLView/2"
]
applyAnimated
:
YES
]
]
;
break
;
case
3
:
[
navigator
openURLAction
:
[
[
TTURLAction
actionWithURLPath
:
@"tt://SQLView/3"
]
applyAnimated
:
YES
]
]
;
break
;
default
:
break
;
}
}
@
end
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#import <Three20/Three20.h>
#import "SingleDate.h"
@
interface
SQLViewController
:
TTTableViewController
{
//记录上一页点击的按钮
int
pageID
;
//获取单例模型的实例
SingleDate
*
instance
;
//数据库对象
FMDatabase
*
db
;
}
@
end
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#import "SQLViewController.h"
#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"
#define DATABASE_NAME @"Temp.db"
@
implementation
SQLViewController
-
(
id
)
initInfo
:
(
int
)
page
{
if
(
self
=
[
super
init
]
)
{
//注解1
pageID
=
page
;
}
return
self
;
}
-
(
void
)
viewDidLoad
{
[
super
viewDidLoad
]
;
//注解2
//图片的地址
NSMutableArray
*
iconurls
=
[
[
[
NSMutableArray
alloc
]
initWithObjects
:
@"bundle://0.jpg"
,
@"bundle://1.jpg"
,
@"bundle://2.jpg"
,
@"bundle://3.jpg"
,
nil
]
autorelease
]
;
//姓名
NSMutableArray
*
names
=
[
[
[
NSMutableArray
alloc
]
initWithObjects
:
@"雨松MOMO"
,
@"RORO娃娃"
,
@"小老虎"
,
@"哇咔咔"
,
nil
]
autorelease
]
;
//性别
NSMutableArray
*
sexs
=
[
[
NSMutableArray
alloc
]
initWithObjects
:
@"男"
,
@"女"
,
@"男"
,
@"女"
,
nil
]
;
//地址
NSMutableArray
*
address
=
[
[
[
NSMutableArray
alloc
]
initWithObjects
:
@"北京市朝阳区"
,
@"北京市西城区"
,
@"北京市海淀区"
,
@"北京市东城区"
,
nil
]
autorelease
]
;
//年龄
NSMutableArray
*
ages
=
[
[
[
NSMutableArray
alloc
]
initWithObjects
:
[
NSNumber
numberWithInt
:
28
]
,
[
NSNumber
numberWithInt
:
27
]
,
[
NSNumber
numberWithInt
:
26
]
,
[
NSNumber
numberWithInt
:
25
]
,
nil
]
autorelease
]
;
//获取单例对象实例
instance
=
[
SingleDate
getInstance
]
;
//读取数据库
db
=
[
instance
loadDB
:
DATABASE_NAME
]
;
//结果集
FMResultSet
*
rs
=
nil
;
//判断当前页面的ID
switch
(
pageID
)
{
case
0
:
//注解3
if
(
!
[
db
tableExists
:
@"Message"
]
)
{
//创建数据库
if
(
[
db
executeUpdate
:
@"CREATE TABLE Message (_id INTEGER PRIMARY KEY AUTOINCREMENT,IconUrl text, Name text, Sex text,Address text,Age integer)"
]
)
{
NSLog
(
@"创建表成功"
)
;
for
(
int
i
=
0
;
i
<
[
iconurls
count
]
;
i
++
)
{
//遍历测试数据数组,然后将测试数据写入数据库当中
if
(
[
db
executeUpdate
:
@"INSERT INTO Message (IconUrl,Name,Sex,Address,Age) VALUES (?,?,?,?,?)"
,
[
iconurls
objectAtIndex
:
i
]
,
[
names
objectAtIndex
:
i
]
,
[
sexs
objectAtIndex
:
i
]
,
[
address
objectAtIndex
:
i
]
,
[
ages
objectAtIndex
:
i
]
]
)
{
NSLog
(
@"插入数据成功"
)
;
}
}
}
}
//返回一个结果集
rs
=
[
db
executeQuery
:
@"SELECT * FROM Message"
]
;
break
;
case
1
:
//判断当数据库中含有Message表时
if
(
[
db
tableExists
:
@"Message"
]
)
{
//注释4
if
(
[
db
executeUpdate
:
@"UPDATE Message SET Name = ? WHERE _id = ? "
,
@"雨松MOMO(已修改)"
,
[
NSNumber
numberWithInt
:
1
]
]
)
{
NSLog
(
@"更新一个数据成功"
)
;
}
if
(
[
db
executeUpdate
:
@"UPDATE Message SET Name = ? , Sex = ? , Address = ? WHERE _id = ? "
,
@"雨松MOMO(已修改)"
,
@"男(已修改)"
,
@"北京市朝阳区(已修改)"
,
[
NSNumber
numberWithInt
:
1
]
]
)
{
NSLog
(
@"更新多条数据成功"
)
;
}
}
//返回一个结果集
rs
=
[
db
executeQuery
:
@"SELECT * FROM Message"
]
;
break
;
case
2
:
//判断当数据库中含有Message表时
if
(
[
db
tableExists
:
@"Message"
]
)
{
//删除数据
if
(
[
db
executeUpdate
:
@"DELETE FROM Message WHERE _id = ?"
,
[
NSNumber
numberWithInt
:
2
]
]
)
{
NSLog
(
@"删除数据成功"
)
;
}
}
//返回一个结果集
rs
=
[
db
executeQuery
:
@"SELECT * FROM Message"
]
;
break
;
case
3
:
//返回一个结果集
rs
=
[
db
executeQuery
:
@"SELECT * FROM Message WHERE _id = ? "
,
[
NSNumber
numberWithInt
:
3
]
]
;
break
;
default
:
break
;
}
NSMutableArray
*
array
=
[
[
[
NSMutableArray
alloc
]
init
]
autorelease
]
;
//注释5
while
(
[
rs
next
]
)
{
[
array
addObject
:
[
TTTableImageItem
itemWithText
:
[
rs
stringForColumn
:
@"Name"
]
imageURL
:
[
rs
stringForColumn
:
@"IconUrl"
]
URL
:
[
NSString
stringWithFormat
:
@"%@%@"
,
@"tt://MessageView/"
,
[
rs
stringForColumn
:
@"_id"
]
]
]
]
;
}
//用完以后一定要关闭
[
rs
close
]
;
//设置列表自适应高度
self
.
variableHeightRows
=
YES
;
//设置列表现实的数据
self
.
dataSource
=
[
TTSectionedDataSource
dataSourceWithArrays
:
@"当前数据库信息"
,
array
,
nil
]
;
}
@
end
|
在获取数据库对象时,这使用到了单例模式,单例模式在程序语言设计中占据重要地位,而在IOS程序开发中也会频繁使用单例模式。举个简单的例子 从viewA 进入 viewB , 但是viewB需要viewA中计算过的一些数据,有些朋友会想那直接在页面切换时将参数传递给viewB不就行了。这样是可以,但是如果程序中频繁的使用这样的操作肯定会出问题,因为它们属于View层,MVC设计模式告诉我们一定不要让数据放在View层。 在这里单例模型就好比控制器层,在viewA中将数据计算完毕后,写入单例对象中的变量中,进入viewB后,在继续从单例中获取刚刚viewA传递进去的数值。这样的思路会清晰很多,避免后期很多麻烦。
SingleDate.h
1
2
3
4
5
6
7
8
9
10
11
12
|
#import <Three20/Three20.h>
#import "FMDatabase.h"
@
interface
SingleDate
:
NSObject
{
}
//获取单例对象
+
(
SingleDate
*
)
getInstance
;
//读取数据库
-
(
FMDatabase
*
)
loadDB
:
(
NSString
*
)
dbName
;
@
end
|
SingleDate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#import "SingleDate.h"
@
implementation
SingleDate
static
SingleDate
*
instance
=
nil
;
//获取单例
+
(
SingleDate
*
)
getInstance
{
@
synchronized
(
self
)
{
if
(
instance
==
nil
)
{
instance
=
[
[
self
alloc
]
init
]
;
}
}
return
instance
;
}
//读取数据库
-
(
FMDatabase
*
)
loadDB
:
(
NSString
*
)
dbName
{
//获取当前程序路径
NSURL
*
appUrl
=
[
[
[
NSFileManager
defaultManager
]
URLsForDirectory
:
NSDocumentDirectory
inDomains
:
NSUserDomainMask
]
lastObject
]
;
NSString
*
dbPath
=
[
[
appUrl
path
]
stringByAppendingPathComponent
:
dbName
]
;
//注释1
FMDatabase
*
db
=
[
FMDatabase
databaseWithPath
:
dbPath
]
;
if
(
!
[
db
open
]
)
{
[
db
release
]
;
NSLog
(
@"数据库未能创建/打开"
)
;
return
nil
;
}
return
db
;
}
@
end
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#import <Three20/Three20.h>
#import "SingleDate.h"
#import "FMDatabase.h"
@
interface
MessageInfoController
:
TTViewController
{
//数据库ID
int
sqlID
;
//单例对象
SingleDate
*
instance
;
//数据库对象
FMDatabase
*
db
;
}
@
end
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
#import "MessageInfoController.h"
#define DATABASE_NAME @"Temp.db"
@
implementation
MessageInfoController
-
(
id
)
initInfo
:
(
int
)
page
{
if
(
self
=
[
super
init
]
)
{
sqlID
=
page
;
}
return
self
;
}
-
(
void
)
viewDidLoad
{
[
super
viewDidLoad
]
;
instance
=
[
SingleDate
getInstance
]
;
db
=
[
instance
loadDB
:
DATABASE_NAME
]
;
//查询数据库的结果集
FMResultSet
*
rs
=
[
db
executeQuery
:
@"SELECT * FROM Message WHERE _id = ? "
,
[
NSNumber
numberWithInt
:
sqlID
]
]
;
if
(
rs
!=
nil
)
{
[
rs
next
]
;
//获取名称
NSString
*
name
=
[
rs
stringForColumn
:
@"Name"
]
;
//获取图片
UIImage
*
image
=
TTIMAGE
(
[
rs
stringForColumn
:
@"IconUrl"
]
)
;
//获取性别
NSString
*
sex
=
[
rs
stringForColumn
:
@"Sex"
]
;
//获取地址
NSString
*
address
=
[
rs
stringForColumn
:
@"Address"
]
;
//获取年龄
int
age
=
[
rs
intForColumn
:
@"Age"
]
;
[
rs
close
]
;
//标题栏名称
self
.
title
=
name
;
//设置视图背景颜色
self
.
view
.
backgroundColor
=
[
UIColor
blackColor
]
;
//创建图片视图
TTImageView
*
imageview
=
[
[
[
TTImageView
alloc
]
initWithFrame
:
CGRectMake
(
100
,
200
,
120
,
120
)
]
autorelease
]
;
//设置图片视图显示的图片资源
imageview
.
defaultImage
=
image
;
//将图片视图加入整个视图当中
[
self
.
view
addSubview
:
imageview
]
;
//将所有文字信息写入视图当中
CGRect
frame
=
CGRectMake
(
10
,
60
,
300
,
100
)
;
TTStyledTextLabel
*
label
=
[
[
[
TTStyledTextLabel
alloc
]
initWithFrame
:
frame
]
autorelease
]
;
NSString
*
labelText
=
[
NSString
stringWithFormat
:
@"姓名:%@ 性别:%@ 地址:%@ 年龄%d"
,
name
,
sex
,
address
,
age
]
;
label
.
text
=
[
TTStyledText
textFromXHTML
:
labelText
]
;
label
.
font
=
[
UIFont
systemFontOfSize
:
22
]
;
[
self
.
view
addSubview
:
label
]
;
}
}
@
end
|