在不同的视图控制器中使用相同的编码集

时间:2013-11-21 09:36:36

标签: ios objective-c

我有一套必须在不同视图控制器中使用的相同编码。我必须做什么,以避免在每个视图控制器中重复编码。我无法在google中找到确切的解决方案。任何一个请帮帮我。

[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//NSLog(@"%d",rowno);

NSString *urlString=[NSString stringWithFormat:@"http://www.tranzlogix.com/tranzlogix_webservice/vehiclelist.php?format=json"];

NSURL *url=[NSURL URLWithString:urlString];
NSData *data=[NSData dataWithContentsOfURL:url];
NSError *error;
//NSLog(@"%@",data);

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
//NSLog(@"%@",json);

results = [json valueForKey:@"posts"];
//NSLog(@"%@", results);
//NSLog(@"Count %d", results.count);

NSArray *res = [results valueForKey:@"post"];
//NSLog(@"%@", res);

Vehicle_No=[res valueForKey:@"vehicle_no"];
//NSLog(@"%@", Vehicle_No);

Vehicle_No_Org =[Vehicle_No objectAtIndex:rowno];
NSString *CellText=[NSString stringWithFormat:@"%@",Vehicle_No_Org];
//NSLog(@"%@",CellText);

//MAP VIEW WebService

NSString *urlMapString=[NSString stringWithFormat:@"http://www.tranzlogix.com/tranzlogix_webservice/map.php?format=json&truckno=%@",CellText];

//NSLog(@"%@",urlMapString);

NSURL *urlMap=[NSURL URLWithString:urlMapString];
NSData *dataMap=[NSData dataWithContentsOfURL:urlMap];
NSError *errorMap;
//NSLog(@"%@",dataMap);

NSDictionary *jsonMap = [NSJSONSerialization JSONObjectWithData:dataMap options:kNilOptions error:&errorMap];
//NSLog(@"%@",jsonMap);

NSArray *resultsMap = [jsonMap valueForKey:@"posts"];
NSLog(@"%@", resultsMap);
//NSLog(@"Count %d", resultsMap.count);

NSArray *resMap = [resultsMap valueForKey:@"post"];
//NSLog(@"%@", resultsMap);

NSArray *latitudeString=[resMap valueForKey:@"latitude"];
NSLog(@"%@", latitudeString);
NSString *latOrgstring = [latitudeString objectAtIndex:0];
NSLog(@"%@", latOrgstring);
double latitude=[latOrgstring doubleValue];
//NSLog(@"latdouble: %f", latitude);


NSArray *longitudeString=[resMap valueForKey:@"longitude"];
NSLog(@"%@", longitudeString);
NSString *longOrgstring = [longitudeString objectAtIndex:0];
NSLog(@"%@", longOrgstring);
double longitude=[longOrgstring doubleValue];
NSLog(@"latdouble: %f", longitude);

这是我在地图视图中的两个以上视图控制器中所需要的以及在表视图中的下一个...

2 个答案:

答案 0 :(得分:4)

使用主代码创建基本视图控制器并创建它的子类。

例如,您的主视图控制器将是:

@interface MainViewController : UIViewController

然后继承它:

@interface OneViewController : MainViewController

这些子类将继承代码。

答案 1 :(得分:0)

Here you can use Custom Delegates in ios..

just create Custom Delegate(Protocol) in AppDelegate or Singleton Class.Here im using AppDelegate

#import <UIKit/UIKit.h>


**AppDeleagte.h**

@protocol parserDelegate <NSObject>


-(void)sendDataToCorrespondingViewController:(id)data andServiceName:(NSString *)serviceName;

@end

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{

}

@property (strong, nonatomic) UIWindow *window;

@property (nonatomic,strong) id <parserDelegate> delegate;


-(void)getMethodURL:(NSString *)urlString andServiceName:(NSString *)serviceName;

@end


**AppDeleagte.m**

//Implementing getMethod() in AppDelegate.m File

-(void)getMethodURL:(NSString *)urlString andServiceName:(NSString *)serviceName;
{



    NSURL *url =[NSURL URLWithString:urlString];
    NSError *error;
    {
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 2), ^{


            NSData *data =[[NSData alloc]initWithContentsOfURL:url];

            if (data == nil) {

                NSLog(@"error bcz data is nil:%@",error.localizedDescription);
            }
            else
            {

                NSError *error;
                id response =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];

                [self.delegate sendDataToCorrespondingViewController:response andServiceName:serviceName];


            }
        });

    }
}


Now import AppDelegate in required ViewController and Create instance For AppDelegate

**ViewController.h**

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

@interface ViewController : UIViewController<parserDelegate>
{

}
@property (nonatomic,strong) AppDelegate *appDelegate;
@end


**ViewController.m**

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize appDelegate;

- (void)viewDidLoad
{

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

    [super viewDidLoad];
}
-(IBAction)getSeviceData:(id)sender
{
    //call AppDeleagte method for Webservice

    [self.appDelegate getMethodURL:@"http://www.tranzlogix.com/tranzlogix_webservice/vehiclelist.php?format=json" andServiceName:@"VehicleList"];

}
//implement Deleagte method
-(void)sendDataToCorrespondingViewController:(id)data andServiceName:(NSString *)serviceName
{
    NSLog(@"response data: %@",data); //Here is the response from websevice
    NSLog(@"serviceName: %@",serviceName); // Here Service name differentiate,if we call 2   webservices in ViewController
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end