posted by 프띠버리 2013. 10. 7. 14:52

AppDelegate.h


#import <UIKit/UIKit.h>


@interface AppDelegate : UIResponder <UIApplicationDelegate>

{

    UIWindow *window;

    UINavigationController *navigationController;

    

    NSString *databaseName;

    NSString *databasePath;

    

    NSMutableArray *foods;

}


@property (strong, nonatomic) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@property (nonatomic, retain) NSMutableArray *foods;


@end





AppDelegate.m


#import "AppDelegate.h"

#import "RootViewController.h"

#import "Food.h"

#import <sqlite3.h>


@implementation AppDelegate

@synthesize window;

@synthesize navigationController;

@synthesize foods;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

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

  

    RootViewController *mainViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];

    self.navigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];

    databaseName = @"Basic.db";

    

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDir = [documentPaths objectAtIndex:0];

    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

    

    NSLog(@"%@", databasePath);

    

    [self checkAndCreateDatabase];

    

    [self readFoodsFromDatabase];

    

    [window addSubview:[navigationController view]];

    [window makeKeyAndVisible];

    return YES;

}


- (void)checkAndCreateDatabase

{

    BOOL success;

    NSFileManager *fileManager = [NSFileManager defaultManager];

    

    success = [fileManager fileExistsAtPath:databasePath];

    

    NSLog(@"error-1-");

    

    if(success) return;

    

    NSLog(@"error-2-");

    

    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

    

    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

}


- (void)readFoodsFromDatabase

{

    sqlite3 *database;

    

    foods = [[NSMutableArray alloc] init];

    

    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)

    {

        const char *sqlStatement = "select * from BasicTbl";

        sqlite3_stmt *compiledStatement;

        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)

        {

            while(sqlite3_step(compiledStatement) == SQLITE_ROW)

            {

                NSString *addr_MOBILE = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];

                NSString *addr_NAME = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];

                NSString *addr_ADDR = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];

                NSString *addr_TEL = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];

                Food *food = [[Food alloc] initWithName:addr_NAME addr:addr_ADDR tel:addr_TEL mobile:addr_MOBILE];

                

                NSLog(@"error-3-, %@", addr_NAME);

                [foods addObject:food];

            }

        }

        sqlite3_finalize(compiledStatement);

    }

    sqlite3_close(database);

}


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

{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell==nil)

    {

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        

    }

    NSDictionary *dict = [foods objectAtIndex:indexPath.row];

    cell.textLabel.text = [dict objectForKey:@"name"];

    

    return cell;

}


@end





Food.h


#import <Foundation/Foundation.h>


@interface Food : NSObject

{

    NSString *name;

    NSString *addr;

    NSString *tel;

    NSString *mobile;

}



@property(nonatomic, retain) NSString *name;

@property(nonatomic, retain) NSString *addr;

@property(nonatomic, retain) NSString *tel;

@property(nonatomic, retain) NSString *mobile;


- (id)initWithName:(NSString *)n addr:(NSString *)a tel:(NSString *)t mobile:(NSString *)m;


@end





Food.m


#import "Food.h"


@implementation Food

@synthesize name, addr, tel, mobile;


- (id)initWithName:(NSString *)n addr:(NSString *)a tel:(NSString *)t mobile:(NSString *)m

{

    self.name = n;

    self.addr = a;

    self.tel = t;

    self.mobile = m;

        

    return self;

}


@end




FoodViewController.h


#import <UIKit/UIKit.h>


@interface FoodViewController : UIViewController

{

    IBOutlet UITextView *addrName;

    IBOutlet UITextView *addrAddr;

    IBOutlet UITextView *addrTel;

    IBOutlet UITextView *addrMobile;

}


@property (nonatomic, retain) IBOutlet UITextView *addrName;

@property (nonatomic, retain) IBOutlet UITextView *addrAddr;

@property (nonatomic, retain) IBOutlet UITextView *addrTel;

@property (nonatomic, retain) IBOutlet UITextView *addrMobile;



@end




FoodViewController.m


#import "FoodViewController.h"


@interface FoodViewController ()


@end


@implementation FoodViewController

@synthesize addrAddr, addrMobile, addrName, addrTel;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end






RootViewController.h


#import <UIKit/UIKit.h>

#import "FoodViewController.h"


@interface RootViewController : UITableViewController

{

    FoodViewController *foodView;

}


@property(nonatomic, retain) FoodViewController *foodView;


@end





RootViewController.m


#import "RootViewController.h"

#import "AppDelegate.h"

#import "Food.h"


@interface RootViewController ()


@end


@implementation RootViewController

@synthesize foodView;


- (id)initWithStyle:(UITableViewStyle)style

{

    self = [super initWithStyle:style];

    if (self) {

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];


    self.title = @"주소록";

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

}


#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

 

    return 1;

}


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

{

  

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];

    return appDelegate.foods.count;

}


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

{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

    

   

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    Food *food = (Food *)[appDelegate.foods objectAtIndex:indexPath.row];

    [cell.textLabel setText:food.name];

    

    return cell;

}

#pragma mark - Table view delegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    Food *food = (Food *)[appDelegate.foods objectAtIndex:indexPath.row];

    

    if(self.foodView == nil)

    {

        FoodViewController *viewController = [[FoodViewController alloc] initWithNibName:@"FoodViewController" bundle:nil];

        self.foodView = viewController;

    }

    [self.navigationController pushViewController:self.foodView animated:YES];

    

    self.foodView.title = [food name];

    

    [self.foodView.addrName setText:[food name]];

    [self.foodView.addrAddr setText:[food addr]];

    [self.foodView.addrTel setText:[food tel]];

    [self.foodView.addrMobile setText:[food mobile]];

    

}


@end


'Programing > IOS' 카테고리의 다른 글

DB를 이용한 간단 메모장 앱  (0) 2013.10.14
간단한 흔들기 제스쳐  (0) 2013.10.07
세그먼트 연습  (0) 2013.10.04
구구단 퀴즈 프로그램 앱  (0) 2013.10.04
선, 사각형 등 도형 그리기 연습  (0) 2013.10.04