Ios 在不同的视图控制器中使用相同的编码集
我有一套相同的编码,必须在不同的视图控制器中使用。我必须做的是,避免在每个视图控制器中重复编码。我无法在google中找到确切的解决方案。有人能帮我吗Ios 在不同的视图控制器中使用相同的编码集,ios,objective-c,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.tranzl
[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);
这是我在两个以上视图控制器中需要的,一个在地图视图中,另一个在表视图中…使用主代码创建一个基本视图控制器,并创建它的子类 例如,主视图控制器将是:
@interface MainViewController : UIViewController
然后将其子类化:
@interface OneViewController : MainViewController
这些子类将继承代码。这里您可以在ios中使用自定义委托。。
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
只需在AppDelegate或Singleton类中创建自定义委托(协议)
#进口
**AppDeleagte.h**
@协议解析器
-(void)将数据发送到相应的ViewController:(id)数据和服务名称:(NSString*)服务名称;
@结束
@接口AppDelegate:UIResponder
{
}
@属性(强,非原子)UIWindow*window;
@属性(非原子,强)id委托;
-(void)getMethodURL:(NSString*)urlString和serviceName:(NSString*)serviceName;
@结束
**AppDeleagte.m**
//在AppDelegate.m文件中实现getMethod()
-(void)getMethodURL:(NSString*)urlString和serviceName:(NSString*)serviceName;
{
NSURL*url=[NSURL URLWithString:urlString];
n错误*错误;
{
调度同步(调度获取全局队列(调度队列优先级高,2)^{
NSData*data=[[NSData alloc]initWithContentsOfURL:url];
如果(数据==nil){
NSLog(@“错误bcz数据为零:%@”,错误.localizedDescription);
}
其他的
{
n错误*错误;
id response=[NSJSONSerialization JSONObjectWithData:data选项:NSJSONReadingMutableLeaves错误:&error];
[self.delegate将数据发送到相应的ViewController:response和serviceName:serviceName];
}
});
}
}
现在在所需的ViewController中导入AppDelegate并为AppDelegate创建实例
**ViewController.h**
#进口
#导入“AppDelegate.h”
@界面ViewController:UIViewController
{
}
@属性(非原子,强)AppDelegate*AppDelegate;
@结束
**ViewController.m**
#导入“ViewController.h”
@界面视图控制器()
@结束
@实现视图控制器
@综合应用程序代表;
-(无效)viewDidLoad
{
self.appDelegate=(appDelegate*)[[UIApplication sharedApplication]delegate];
self.appDelegate.delegate=self;
[超级视图下载];
}
-(iAction)getSeviceData:(id)发送方
{
//调用Webservice的AppDeleagte方法
[self.appDelegate getMethodURL:@”http://www.tranzlogix.com/tranzlogix_webservice/vehiclelist.php?format=json“和服务名称:@“车辆列表”];
}
//实现delegate方法
-(void)将数据发送到相应的ViewController:(id)数据和服务名称:(NSString*)服务名称
{
NSLog(@“响应数据:%@”,数据);//这是来自WebDevice的响应
NSLog(@“serviceName:%@”,serviceName);//如果我们在ViewController中调用2个Web服务,则服务名称在这里有所区别
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
@结束
继承?哎呀p请指定同一组编码的含义,并添加一些代码。您可以为通用编码创建baseviewcontroller,并将baseviewcontroller继承到视图控制器中。是,如果我在主视图控制器和下一个视图控制器中有编码,我必须使用纬度和经度值在地图中定位。为此,我必须做的是将它们创建为公共变量(在接口文件.h中声明),它们将被继承。我必须在主视图控制器中声明它。h对吗?是的,在那里声明它们为属性,它们将被继承。非常感谢…真的很有帮助。。。