appdelegate.h
//
//
UsingJSONAppDelegate.h
//
UsingJSON
//
//
Created by Wei-Meng Lee on 28/8/11.
//
Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@class UsingJSONViewController;
@interface UsingJSONAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong,
nonatomic) UIWindow
*window;
@property (strong,
nonatomic) UsingJSONViewController
*viewController;
@property (strong,nonatomic)UINavigationController
*nav;
@end
appdelegate.m
//
// UsingJSONAppDelegate.m
// UsingJSON
//
// Created by Wei-Meng Lee on
28/8/11.
// Copyright (c) 2011
__MyCompanyName__. All rights reserved.
//
#import "UsingJSONAppDelegate.h"
#import "UsingJSONViewController.h"
@implementation UsingJSONAppDelegate
@synthesize
window = _window;
@synthesize
viewController = _viewController;
@synthesize
nav;
- (void)dealloc
{
[_window
release];
[_viewController
release];
[super dealloc];
}
- (BOOL)application:(UIApplication
*)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
self.window = [[[UIWindow
alloc] initWithFrame:[[UIScreen mainScreen]
bounds]] autorelease];
// Override
point for customization after application launch.
self.viewController = [[[UsingJSONViewController
alloc] initWithNibName:@"UsingJSONViewController" bundle:nil] autorelease];
nav=[[UINavigationController alloc]initWithRootViewController:_viewController];
self.window.rootViewController
= self.nav;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
/*
Sent when the application is
about to move from active to inactive state. This can occur for certain types
of temporary interruptions (such as an incoming phone call or SMS message) or
when the user quits the application and it begins the transition to the
background state.
Use this method to pause
ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games
should use this method to pause the game.
*/
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release
shared resources, save user data, invalidate timers, and store enough
application state information to restore your application to its current state
in case it is terminated later.
If your application supports
background execution, this method is called instead of
applicationWillTerminate: when the user quits.
*/
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the
transition from the background to the inactive state; here you can undo many of
the changes made on entering the background.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
Restart any tasks that were
paused (or not yet started) while the application was inactive. If the
application was previously in the background, optionally refresh the user
interface.
*/
}
- (void)applicationWillTerminate:(UIApplication *)application
{
/*
Called when the application
is about to terminate.
Save data if appropriate.
See also
applicationDidEnterBackground:.
*/
}
@end
vc.h
//
// UsingJSONViewController.h
// UsingJSON
//
// Created by Wei-Meng Lee on
28/8/11.
// Copyright (c) 2011
__MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "SBJson.h"
#import "custom.h"
#import "singleton.h"
#import "myViewController.h"
@interface
UsingJSONViewController : UIViewController
<NSURLConnectionDelegate>
{
IBOutlet
UITableView *tableview;
//NSURLConnection
*conn;
NSMutableData
*webData;
NSDictionary
*parsedJSON;
NSMutableArray
*CatId;
NSMutableArray
*CategoryName;
NSMutableArray
*BTID;
}
@property (nonatomic, retain)
UITextField *txtLat;
@property (nonatomic, retain)
UITextField *txtLng;
@property (strong,nonatomic)
UITableView *tableview;
@property(nonatomic,strong)
NSMutableArray *CatId;
@property(nonatomic,strong)NSMutableArray *CategoryName;
@property(nonatomic,strong)NSMutableArray *BTID;
-(IBAction)btnGetWeather;
@end
vc.m
//
// UsingJSONViewController.m
// UsingJSON
//
// Created by Wei-Meng Lee on
28/8/11.
// Copyright (c) 2011
__MyCompanyName__. All rights reserved.
//
#import "UsingJSONViewController.h"
@implementation UsingJSONViewController
@synthesize
tableview;
@synthesize
CatId,CategoryName,BTID;
-(IBAction)btnGetWeather
{
NSMutableURLRequest
*request = [[[NSMutableURLRequest alloc] init] autorelease];
// [request
setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://118.139.178.115/IMLocal/DeviceServices/FetchCompCategoryByCategoryId.aspx"]]];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://118.139.178.115/SkilledTradesDepot/DevicesServices/FetchCategoryByTrade.aspx"]]];
// [request
setHTTPMethod:@"POST"];
// [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
// [request setHTTPBody:postData];
NSURLConnection
*conn = [[NSURLConnection alloc]initWithRequest:request
delegate:self];
if(conn)
{
NSLog(@"Connection Successful");
}
else
{
NSLog(@"Connection could not be made");
}
webData
= [[NSMutableData alloc]init];
}
-(void) connection:(NSURLConnection
*)connection
didReceiveResponse:(NSURLResponse *) response{
[webData
setLength: 0];
}
-(void) connection:(NSURLConnection
*)connection
didReceiveData:(NSData
*) data
{
[webData
appendData:data];
}
-(void)handleResponce:(NSString
*)data
{
SBJsonParser
*json = [[SBJsonParser new] autorelease];
NSError
*jsonError;
parsedJSON
= [json objectWithString:data error:&jsonError];
NSLog(@"parse...%@",parsedJSON);
//NSDictionary*
menu = [parsedJSON objectForKey:@"DTOCollection"];
NSArray
*array =[parsedJSON objectForKey:@"DTOCollection"];
NSLog(@"%@",array);
NSDictionary*
menu = [array objectAtIndex:1];
NSDictionary*
menu1 = [menu objectForKey:@"DTOJson"];
NSLog(@"%@",menu1);
// CatId= [[NSMutableArray alloc]init];
[singleton
show].array=[[NSMutableArray alloc]init ];
for(int counter =0;counter<
menu1.count;counter++)
{
NSString*
str = [NSString stringWithFormat:@"%i", counter];
//
NSDictionary* menu1 = [menu objectForKey:str];
NSDictionary*
menuOne =[menu1 objectForKey:str];
NSLog(@"%@",menuOne);
NSString
*strbtid=[menuOne objectForKey:@"BTID"];
NSLog(@"BTID----%@",strbtid);
NSString
*strcatid=[menuOne objectForKey:@"CatId"];
NSLog(@"CatID----%@",strcatid);
NSString
*strcategoryname=[menuOne objectForKey:@"CategoryName"];
NSLog(@"CategoryName---%@",strcategoryname);
[ [singleton
show].array
addObject:strcategoryname];
[[singleton
show].array
addObject:strbtid];
[[singleton
show].array
addObject:strcatid];
}
//myViewController *my=[[myViewController alloc]init ];
//[my.tbl reloadData ];
}
/*-(IBAction)ButtonTapped:(id)sender
{
NSString *strURL=[NSString
stringWithFormat:@"URl Name here"];
NSURL *url=[NSURL
URLWithString:strURL];
self.request=[NSURLRequest
requestWithURL:url]
self.nsCon=[[NSURLConnection
alloc] initWithRequest:request delegate:self];
if(self.nsCon)
{
self.webData=[[NSMutableData alloc] init];
}
else
{
UIAlertView *alert =
[[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"Error",@"")
message:NSLocalizedString(@"Not Connected Other View !!",@"")
delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",@"")
otherButtonTitles:nil];
[alert show];
[alert release];
}
}*/
-(void) connection:(NSURLConnection
*)connection
didFailWithError:(NSError
*) error {
[webData
release];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString
*strResult = [[NSString alloc] initWithBytes:[webData mutableBytes]
length:[webData
length]
encoding:NSUTF8StringEncoding];
NSLog(@"DONE. Received Bytes: %@", strResult);
[self handleResponce:strResult];
// NSDictionary *result =
[strResult JSONValue];
// for (id theKey in result) {
// NSDictionary
*detailedItems = [result objectForKey:theKey];
// UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:@"Result"
//
message:strResult
//
delegate:self
//
cancelButtonTitle:@"OK"
//
otherButtonTitles:nil];
// [alert show];
// [alert release];
//
// NSLog(@"Key is %@,
Value is %@", theKey, detailedItems);
// for (id detailedKey in
detailedItems) {
// id detailedValue =
[detailedItems objectForKey:detailedKey];
// NSLog(@"Key is
%@, Value is %@", detailedKey, detailedValue);
// }
// }
[strResult release];
[webData
release];
}
-(void) dealloc
{
// [txtLat
release];
// [txtLng
release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any
cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
-(IBAction)loadData
{
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self btnGetWeather];
// Do
any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any
retained subviews of the main view.
// e.g.
self.myOutlet = nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView
*)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (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]autorelease];
}
NSLog(@"xcxcx%@",[singleton
show].lbl);
cell.textLabel.text=[CatId objectAtIndex:indexPath.row];
NSLog(@"xcxcx%@",[CatId
objectAtIndex:indexPath.row]);
return
cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView
*)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{
myViewController
*my=[[myViewController alloc]init];
[self.navigationController pushViewController:my
animated:YES];
NSLog(@"xcxcx%@",[CatId objectAtIndex:indexPath.row]);
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super
viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super
viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for
supported orientations
return
(interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
myvc.h
//
// myViewController.h
// UsingJSON
//
// Created by ugsa on 9/11/12.
// Copyright (c) 2012
__MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "custom.h"
#import "singleton.h"
@interface
myViewController : UIViewController
{ IBOutlet UITableView
*tbl;
NSMutableArray
*receivedData;
NSString
*strCat;
NSString
*str1;
NSString
*str2;
}
@property(strong,nonatomic)UITableView *tbl;
@property(strong,nonatomic)NSMutableArray *receivedData;
@property(strong,nonatomic)NSString *strCat;
@property(strong,nonatomic)NSString *str1;
@property(strong,nonatomic)NSString *str2;
@end
myvc.m
//
// myViewController.m
// UsingJSON
//
// Created by ugsa on 9/11/12.
// Copyright (c) 2012
__MyCompanyName__. All rights reserved.
//
#import "myViewController.h"
@interface myViewController ()
@end
@implementation myViewController
@synthesize
tbl,receivedData,strCat;
- (void)viewDidLoad
{
[super viewDidLoad];
}
//
Uncomment the following line to preserve selection between presentations.
//
self.clearsSelectionOnViewWillAppear = NO;
//
Uncomment the following line to display an Edit button in the navigation bar
for this view controller.
// self.navigationItem.rightBarButtonItem
= self.editButtonItem;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom
initialization
}
return self;
}
- (void)viewDidUnload
{
[super
viewDidUnload];
// Release
any retained subviews of the main view.
// e.g.
self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return
(interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return
1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return
[[singleton show].array count];
}
- (UITableViewCell
*)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static
NSString *CellIdentifier = @"Cell";
custom
*cell = [tbl dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell = [[[custom
alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
NSLog(@"xcxcx%@",[singleton
show].array);
// NSLog(@"xcxcx%@",[singleton
show].lbl);
NSString
*str= [NSString stringWithFormat:@"%@", [[singleton
show].array
objectAtIndex:indexPath.row]];
NSLog(@"xcxcx%@",str);
cell.lbl.text=str;
// NSString
*str1= [NSString stringWithFormat:@"%@", [[singleton show].array
objectAtIndex:indexPath.row]];
//
NSLog(@"xcxcx%@",str1);
//
cell.lbl1.text=str1;
// NSString
*str2= [NSString stringWithFormat:@"%@", [[singleton show].array
objectAtIndex:indexPath.row]];
//NSLog(@"xcxcx%@",str2);
//
cell.lbl2.text=str2;
return
cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"xcxcx%@",[singleton
show].array);
}
-(void)dealloc
{
//[firstViewController
release];
[super
dealloc];
}
@end
single
ton.com
//
// singleton.h
// UsingJSON
//
// Created by ugsa on 9/13/12.
// Copyright (c) 2012
__MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface
singleton : NSObject
{
NSString
*tf;
NSString
*lbl;
NSMutableArray
*array;
}
@property (strong, nonatomic) NSString
*tf;
@property (strong, nonatomic)
NSString *lbl;
@property (strong,nonatomic)NSMutableArray *array;
+(singleton *)show;
@end
singleton.m
//
// singleton.m
// UsingJSON
//
// Created by ugsa on 9/13/12.
// Copyright (c) 2012
__MyCompanyName__. All rights reserved.
//
#import "singleton.h"
@implementation singleton
@synthesize
tf,lbl;
@synthesize array;
static singleton*
_show=nil;
+(singleton *)show
{
@synchronized([singleton class])
{
if (!_show)
_show=[[self alloc] init];
return
_show;
}
return nil;
}
+(id)alloc
{
@synchronized([singleton class])
{
NSAssert(_show == nil, @"Attempted to allocate a second instance of a
singleton.");
_show
= [super alloc];
return
_show;
}
return nil;
}
-(id)init {
self = [super init];
if (self != nil) {
}
return self;
}
@end
custom.h
//
// custom.h
// UsingJSON
//
// Created by ugsa on 9/17/12.
// Copyright (c) 2012
__MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface
custom : UITableViewCell
{
IBOutlet UILabel *lbl;
IBOutlet
UILabel *lbl1;
IBOutlet
UILabel *lbl2;
}
@property(strong,nonatomic)UILabel *lbl;
@property(strong,nonatomic)UILabel *lbl1;
@property(strong,nonatomic)UILabel *lbl2;
@end
custom.m
//
// custom.m
// UsingJSON
//
// Created by ugsa on 9/17/12.
// Copyright (c) 2012
__MyCompanyName__. All rights reserved.
//
#import "custom.h"
@implementation custom
@synthesize
lbl,lbl1,lbl2;
- (id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style
reuseIdentifier:reuseIdentifier];
if (self) {
lbl
= [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
lbl.textColor = [UIColor
grayColor];
[self.contentView addSubview:lbl];
//lbl1
= [[UILabel alloc]initWithFrame:CGRectMake(30, 10, 120, 40)];
//lbl1.textColor
= [UIColor grayColor];
//[self.contentView
addSubview:lbl1];
//lbl2
= [[UILabel alloc]initWithFrame:CGRectMake(50, 10, 140, 50)];
//lbl2.textColor
= [UIColor grayColor];
//[self.contentView
addSubview:lbl2];
}
//
Initialization code
return self;
}
- (void)setSelected:(BOOL)selected
animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure
the view for the selected state
}
@end