2013-11-21 34 views
-1

我有一組相同的編碼必須在不同的視圖控制器中使用。我必須做什麼,以避免在每個視圖控制器中重複編碼。我無法找到在谷歌的確切解決方案。請任何人幫助我。在不同的視圖控制器中使用相同的編碼集

[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); 

這就是我需要兩個以上的視圖控制器一個在地圖視圖和表視圖下一個...

+0

繼承? OOPs ..:P – Amar

+0

請詳細說明同一套編碼是什麼意思,並添加一些代碼 – suhit

+0

您可以創建一個baseview控制器,用於常見編碼,並將baseview控制器繼承到您的視圖控制器。http://www.techotopia.com /index.php/Objective-C_Inheritance – sabeer

回答

4

與主代碼創建一個基本視圖控制器和創建它的子類。

例如,您的主視圖控制器是:

@interface MainViewController : UIViewController 

然後它的子類:

@interface OneViewController : MainViewController 

這些子類將繼承的代碼。

+0

是的,如果我在主視圖控制器和下一個視圖控制器中編碼,我必須使用緯度和經度值在地圖中定位。爲此,我必須做的。 – Sukesh

+0

創建它們作爲公共變量(聲明它們在你的接口文件中),它們將被繼承 –

+0

我必須在mainviewcontroller.h中聲明它的權利嗎? – Sukesh

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 
相關問題