②
General->翻到頁面最底部->找到
- (
BOOL
)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *vc = [[ViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = nav;
return
YES;
}
@interface CodeModel : NSObject
@property (nonatomic,strong)NSString *codeName;
@property (nonatomic,assign)
int
codeId;
@end
+(instancetype)sharedHandle;
-(
void
)insertCode:(CodeModel *)codename;
-(NSMutableArray*)getAll;
#import "CodeHandle.h"
static
FMDatabase *fmdb;
static
CodeHandle *_codeHandle;
@implementation CodeHandle
+(instancetype)sharedHandle
{
static
dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_codeHandle = [[CodeHandle alloc]init];
[_codeHandle initDB];
});
return
_codeHandle;
}
+(instancetype)allocWithZone:(
struct
_NSZone *)zone
{
if
(!_codeHandle) {
_codeHandle = [super allocWithZone:zone];
}
return
_codeHandle;
}
-(
void
)initDB
{
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path = [docPath stringByAppendingPathComponent:@
"sjk.sqlite"
] ;
NSLog(@
"path = %@"
,path);
fmdb = [[FMDatabase alloc]initWithPath:path];
if
([fmdb open]) {
[fmdb executeUpdate:@
"CREATE TABLE CODE(codeId INTEGER PRIMARY KEY AUTOINCREMENT,codeName TEXT)"
];
[fmdb close];
}
else
NSLog(@
"建立數據表失敗!"
);
}
-(
void
)insertCode:(CodeModel *)codename
{
[fmdb open];
BOOL
insertSql = [fmdb executeUpdate:@
"INSERT INTO CODE VALUES(null,?)"
,codename.codeName];
if
(insertSql) {
NSLog(@
"添加成功"
);
}
else
{
NSLog(@"添加失敗");
}
[fmdb close];
}
-(NSMutableArray *)getAll
{
NSMutableArray *arr = [NSMutableArray array];
[fmdb open];
FMResultSet *fmset = [[FMResultSet alloc]init];
fmset = [fmdb executeQuery:@
"SELECT *FROM CODE"
];
while
([fmset next]) {
int
codeId = [fmset intForColumn:@
"codeId"
] ;
NSString *codeName = [fmset stringForColumn:@
"codeName"
];
CodeModel *cm = [[CodeModel alloc]init];
cm.codeId = codeId;
cm.codeName = codeName;
[arr addObject:cm];
}
return
arr;
}
#import "ViewController.h"
@interface ViewController ()
{
UITextField *inputTF;
UIImageView *imgView;
}
@end
@implementation ViewController
- (
void
)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@
"tiaozhuan"
style:UIBarButtonItemStylePlain target:self action:@selector(tiaozhuan)];
self.navigationItem.rightBarButtonItem =right;
inputTF = [[UITextField alloc]initWithFrame:CGRectMake(100, 150, 200, 30)];
inputTF.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:inputTF];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(300, 150, 50, 30);
[btn setTitle:@
"生成"
forState:UIControlStateNormal];
[btn addTarget:self action:@selector(addCode) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
imgView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 200, 200)];
[self.view addSubview:imgView];
}
-(
void
)tiaozhuan
{
ShowResultTableViewController *show = [[ShowResultTableViewController alloc]init];
[self.navigationController pushViewController:show animated:YES];
}
-(
void
)addCode
{
UIImage *img = [QRCodeGenerator qrImageForString:inputTF.text imageSize:200.0];
imgView.image = img;
NSString *imgPath = [NSString stringWithFormat:@
"%@/%@.png"
,NSHomeDirectory(),inputTF.text];
[UIImagePNGRepresentation(img) writeToFile:imgPath atomically:YES];
NSLog(@
"imgPath = %@"
,imgPath);
CodeModel *cm = [[CodeModel alloc]init];
cm.codeName = inputTF.text;
[[CodeHandle sharedHandle]insertCode:cm];
NSLog(@
"codeId = %d"
,cm.codeId);
}
#import "ShowResultTableViewController.h"
#import "CodeModel.h"
#import "CodeHandle.h"
@interface ShowResultTableViewController ()
{
NSMutableArray *arr;
}
@end
@implementation ShowResultTableViewController
- (
void
)viewDidLoad {
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return
arr.count;
}
-(
void
)viewWillAppear:(
BOOL
)animated
{
[super viewWillAppear:animated];
arr = [[CodeHandle sharedHandle]getAll];
NSLog(@
"arr = %@"
,arr);
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static
NSString *str = @
"sdf"
;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str ];
if
(!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
}
CodeModel *cm = arr[indexPath.row];
cell.textLabel.text = cm.codeName;
NSString *imgPath = [NSString stringWithFormat:@
"%@/%@.png"
,NSHomeDirectory(),cm.codeName];
UIImage *img = [UIImage imageWithContentsOfFile:imgPath];
cell.imageView.image = img;
return
cell;
}
@end