以下为主要代码
#import@interface FirstViewController : UIViewController
{
UIButton *yunda;
UIButton *ems;
UIButton *yuantong;
UIButton *shentong;
UIButton *more;
UIButton *shunfeng;
UIButton *tiantian;
}
- (void)select;
@end
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "OtherViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
-(void)loadView{
[super loadView];
UIColor *testColor= [UIColor colorWithRed:150/255.0 green:200/255.0 blue:255/255.0 alpha:1];
self.view.backgroundColor = testColor;
[self select];
}
- (void)select{
yunda = [[UIButton alloc]initWithFrame:CGRectMake(30, 100, 140, 50)];
// yunda=[UIButton buttonWithType:UIButtonTypeCustom];
[yunda setBackgroundImage:[UIImage imageNamed:@"yunda"] forState:UIControlStateNormal];
[yunda setTag:1];
[yunda addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yunda];
ems = [[UIButton alloc]initWithFrame:CGRectMake(205, 100, 140, 50)];
[ems setBackgroundImage:[UIImage imageNamed:@"EMS"] forState:UIControlStateNormal];
[ems setTag:2];
[ems addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:ems];
//圆通
yuantong = [[UIButton alloc]initWithFrame:CGRectMake(30, 195, 140, 50)];
[yuantong setBackgroundImage:[UIImage imageNamed:@"yuantong"] forState:UIControlStateNormal];
[yuantong setTag:3];
[yuantong addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yuantong];
//申通
shentong = [[UIButton alloc]initWithFrame:CGRectMake(205, 195, 140, 50)];
[shentong setBackgroundImage:[UIImage imageNamed:@"shentong"] forState:UIControlStateNormal];
[shentong setTag:4];
[shentong addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:shentong];
//顺丰
shunfeng = [[UIButton alloc]initWithFrame:CGRectMake(30, 280, 140, 50)];
[shunfeng setBackgroundImage:[UIImage imageNamed:@"shunfeng"] forState:UIControlStateNormal];
[shunfeng setTag:5];
[shunfeng addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:shunfeng];
//天天
tiantian = [[UIButton alloc]initWithFrame:CGRectMake(205, 280, 140, 50)];
[tiantian setBackgroundImage:[UIImage imageNamed:@"tiantian"] forState:UIControlStateNormal];
[tiantian setTag:6];
[tiantian addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:tiantian];
more = [[UIButton alloc]initWithFrame:CGRectMake(35, 380, 300, 60)];
[more setBackgroundColor:[UIColor greenColor]];
[more setTitle:@"更多快递..." forState:UIControlStateNormal];
[more setTag:7];
[more addTarget:self action:@selector(clickTwo:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:more];
}
- (void)click:(UIButton *)buton{
SecondViewController *viewScend = [[SecondViewController alloc] init];
viewScend.title = @"查询";
switch ([buton tag]) {
case 1:
[viewScend companyName:@"yunda" withChinese:@"韵达快递"];
break;
case 2:
[viewScend companyName:@"ems" withChinese:@"EMS"];
break;
case 3:
[viewScend companyName:@"yuantong" withChinese:@"圆通快递"];
break;
case 4:
[viewScend companyName:@"shentong" withChinese:@"申通快递"];
break;
case 5:
[viewScend companyName:@"shunfeng" withChinese:@"顺丰快递"];
break;
case 6:
[viewScend companyName:@"tiantian" withChinese:@"天天快递"];
break;
}
[self.navigationController pushViewController:viewScend animated:YES];
}
- (void)clickTwo:(UIButton *)button{
OtherViewController *viewOfOther = [[OtherViewController alloc]init];
viewOfOther.title = @"其他快递";
[self.navigationController pushViewController:viewOfOther animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
#import@interface SecondViewController : UIViewController
{
UITextField *setNumber;
UITextField *setName;
}
@property NSString *companyName;
- (void)companyName:(NSString *)name withChinese:(NSString *)chinese;
@end
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)companyName:(NSString *)name withChinese:(NSString *)chinese{
NSLog(@"%@,%@",name,chinese);
self.companyName = name;
[self viewOfInput:name addChinese:chinese];
}
- (void)viewOfInput:(NSString *)comName addChinese:cName{
UILabel *name=[[UILabel alloc]initWithFrame:CGRectMake(20, 150, 60, 50)];
[name setTextColor:[UIColor blackColor]];
[name setBackgroundColor:[UIColor clearColor]];
[name setText:@"公司名"];
UILabel *number=[[UILabel alloc]initWithFrame:CGRectMake(20, 200, 60, 50)];
[number setTextColor:[UIColor blackColor]];
[number setBackgroundColor:[UIColor clearColor]];
[number setText:@"运单号"];
[self.view addSubview:name];
[self.view addSubview:number];
setName=[[UITextField alloc]initWithFrame:CGRectMake(80, 160,200, 30)];
[setName setBackgroundColor:[UIColor whiteColor]];
[setName setPlaceholder:@"输入快递名 如:yunda"];
[setName setText:cName];
[setName setTextColor:[UIColor grayColor]];
[setName setBorderStyle:UITextBorderStyleRoundedRect];
[setName setFont:[UIFont systemFontOfSize:18]];
[setName resignFirstResponder];
setName.delegate =(id)self;
setNumber=[[UITextField alloc]initWithFrame:CGRectMake(80,210, 200, 30)];
[setNumber setBackgroundColor:[UIColor whiteColor]];
[setNumber setPlaceholder:@"输入运单号"];
[setNumber setBorderStyle:UITextBorderStyleRoundedRect];
[setNumber setReturnKeyType:UIReturnKeyDone];
[setNumber setTextColor:[UIColor greenColor]];
[setNumber setFont:[UIFont systemFontOfSize:18]];
setNumber.delegate =(id)self;
[self.view addSubview:setNumber];
[self.view addSubview:setName];
UIColor *testColor= [UIColor colorWithRed:150/255.0 green:200/255.0 blue:255/255.0 alpha:1];
//查询按钮
UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonOne.frame=CGRectMake(90, 260, 150, 50);
[buttonOne setBackgroundColor:testColor];
[buttonOne setTitle:@"查询" forState:UIControlStateNormal];
buttonOne.titleLabel.font = [UIFont systemFontOfSize:20];
[buttonOne setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[buttonOne addTarget:self action:@selector(clic:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonOne];
}
-(void)resume{
[self.view setFrame:CGRectMake(0, 0, 320, 480)];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[setName resignFirstResponder];
[setNumber resignFirstResponder];
}
#pragma mark texfiledDelegte
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if (textField == setNumber) {
[self.view setFrame:CGRectMake(0, -40, 320, 480)];
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
[self resume];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
[textField resignFirstResponder];
[self resume];
}
-(void)clic:(UIButton *)button{
ThirdViewController *viewThird = [[ThirdViewController alloc] init];
viewThird .view.backgroundColor=[UIColor greenColor];
[viewThird flashDataWithName:self.companyName withNumber:[setNumber text]];
viewThird.title = @"查询结果";
[self.navigationController pushViewController:viewThird animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
#import@interface ThirdViewController : UIViewController
@property(nonatomic)NSMutableData *receiveData;
@property(nonatomic)NSURLRequest *request;
@property(nonatomic)UIColor *testColor;
@property(nonatomic)UILabel *lableWrong;
- (void)flashDataWithName:(NSString *)strName withNumber:(NSString *)strNum;
- (void)showInView:(NSArray *)dic;
@end
#import "ThirdViewController.h"
@interface ThirdViewController ()
@property NSMutableDictionary *array;
@end
@implementation ThirdViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)flashDataWithName:(NSString *)strName withNumber:(NSString *)strNum{
NSString *urlName = [[NSString alloc]init];
NSString *urltext = [[NSString alloc]init];
NSString *urlNum = [[NSString alloc]init];
NSLog(@"urlNum=%@",urlNum);
if(!(strName == urltext)){
urlName = [NSString stringWithFormat:@"?company=%@",strName];
}
if(!(strNum == urltext)){
urlNum = [NSString stringWithFormat:@"&id=%@",strNum];
}
NSString *strUrl = [NSString stringWithFormat:@"http://www.kuaidi100.com/query?type=%@&postid=%@",strName,strNum];
NSURL *urlOne = [NSURL URLWithString:strUrl];
self.request=[NSURLRequest requestWithURL:urlOne];
NSData*data=[NSData dataWithContentsOfURL:urlOne];
NSDictionary *Dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",Dict);
NSURLSession *session=[NSURLSession sharedSession];
NSURLSessionDataTask *dataTask=[session dataTaskWithRequest:self.request
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
_array=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
if (error) {
NSLog(@"error:%@",error.description);
}
else
{
}
}];
[dataTask resume];
[self showInView:Dict[@"data"]];
}
#pragma mark - NSURLConnectionDataDelegate
//收到服务器回应的时候调用此方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
_receiveData=[NSMutableData alloc];
}
//接收到服务器传输数据的时候调用,此方法根据数据大小执行若干次
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[_receiveData appendData:data];
}
//请求完之后调用
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
UIColor *testColor= [UIColor colorWithRed:150/255.0 green:200/255.0 blue:255/255.0 alpha:1];
_lableWrong = [[UILabel alloc]initWithFrame:CGRectMake(50, 150, 220, 100)];
[_lableWrong setTextAlignment:NSTextAlignmentCenter];
_lableWrong.backgroundColor = testColor;
_lableWrong.numberOfLines = 0;
NSLog(@"connectionDidFinishLoading");
NSString *infoString = [[NSString alloc]initWithData:_receiveData encoding:NSUTF8StringEncoding];
NSLog(@"返回的json数据为:%@",infoString);
//返回信息处理
if ([infoString isEqualToString:@"null"]) {
NSLog(@"查询成功但运单过期或没有物流信息");
_lableWrong.text = @"查询成功但运单过期或没有物流信息";
[self.view addSubview:_lableWrong];
}
else if([infoString isEqualToString:@"Param_Error"]){
NSLog(@"缺少参数");
_lableWrong.text = @"缺少参数";
[self.view addSubview:_lableWrong];
}
else if([infoString isEqualToString:@"Illegal_ID"]){
NSLog(@"运单号或快递公司编码无效");
_lableWrong.text = @"运单号或快递公司编码无效";
[self.view addSubview:_lableWrong];
}
else if([infoString isEqualToString:@"Server_Error"]){
NSLog(@"服务器错误");
_lableWrong.text = @"运单号或快递公司编码无效";
[self.view addSubview:_lableWrong];
}
else{
NSLog(@"成功");
NSArray *array = [NSJSONSerialization JSONObjectWithData:_receiveData options: NSJSONReadingAllowFragments error:nil];
[self showInView:array];
}
}
- (void)showInView:(NSArray *)array{
int i=0;
for(NSDictionary *dic in array){
//lable显示时间
UILabel *showTime = [[UILabel alloc]initWithFrame:CGRectMake(10, 65+i*30, 30, 15)];
showTime.font = [UIFont systemFontOfSize:10];
[showTime setTextColor:[UIColor redColor]];
[showTime setText:@"时间:"];
[self.view addSubview:showTime];
//lable显示状态
UILabel *showDetail = [[UILabel alloc]initWithFrame:CGRectMake(10, 80+i*30, 30, 15)];
showDetail.font = [UIFont systemFontOfSize:10];
[showDetail setTextColor:[UIColor greenColor]];
[showDetail setText:@"状态:"];
[self.view addSubview:showDetail];
//label显示具体时间
showTime = [[UILabel alloc]initWithFrame:CGRectMake(40, 65+i*30, 280, 15)];
showTime.font = [UIFont systemFontOfSize:10];
[showTime setBackgroundColor:[UIColor redColor]];
[showTime setText:[dic objectForKey:@"Time"]];
[self.view addSubview:showTime];
//lable显示具体状态
showDetail = [[UILabel alloc]initWithFrame:CGRectMake(40, 80+i*30, 280, 15)];
showDetail.font = [UIFont systemFontOfSize:10];
[showDetail setBackgroundColor:[UIColor greenColor]];
[showDetail setText:[dic objectForKey:@"Detail"]];
[self.view addSubview:showDetail];
i++;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
#import@interface OtherViewController : UIViewController
@end
#import "OtherViewController.h"
#import "SecondViewController.h"
@interface OtherViewController ()
@end
@implementation OtherViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIColor *testColor= [UIColor colorWithRed:150/255.0 green:200/255.0 blue:255/255.0 alpha:1];
UILabel *A = [[UILabel alloc]initWithFrame:CGRectMake(20, 80, 10, 20)];
[A setText:@"A"];
[A setFont:[UIFont systemFontOfSize:10]];
[A setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:A];
UIButton *anxinda = [[UIButton alloc]initWithFrame:CGRectMake(40, 80, 70, 20)];
[anxinda setTitle:@"安信达快递" forState:UIControlStateNormal];
anxinda.titleLabel.font = [UIFont systemFontOfSize:10];
[anxinda setBackgroundColor:testColor];
[anxinda addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[anxinda setTag:1];
[self.view addSubview:anxinda];
UIButton *anjie = [[UIButton alloc]initWithFrame:CGRectMake(120, 80, 70, 20)];
[anjie setTitle:@"安捷快递" forState:UIControlStateNormal];
anjie.titleLabel.font = [UIFont systemFontOfSize:10];
[anjie setBackgroundColor:testColor];
[anjie addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[anjie setTag:2];
[self.view addSubview:anjie];
UILabel *B = [[UILabel alloc]initWithFrame:CGRectMake(20, 110, 10, 20)];
[B setText:@"B"];
[B setFont:[UIFont systemFontOfSize:10]];
[B setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:B];
UIButton *biaoji = [[UIButton alloc]initWithFrame:CGRectMake(40, 110, 70, 20)];
[biaoji setTitle:@"彪记快递" forState:UIControlStateNormal];
biaoji.titleLabel.font = [UIFont systemFontOfSize:10];
[biaoji setBackgroundColor:testColor];
[biaoji addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[biaoji setTag:3];
[self.view addSubview:biaoji];
UIButton *bnt = [[UIButton alloc]initWithFrame:CGRectMake(120, 110, 70, 20)];
[bnt setTitle:@"BNT" forState:UIControlStateNormal];
bnt.titleLabel.font = [UIFont systemFontOfSize:10];
[bnt setBackgroundColor:testColor];
[bnt addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[bnt setTag:4];
[self.view addSubview:bnt];
UILabel *C = [[UILabel alloc]initWithFrame:CGRectMake(20, 140,10, 20)];
[C setText:@"C"];
[C setFont:[UIFont systemFontOfSize:10]];
[C setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:C];
UIButton *coe = [[UIButton alloc]initWithFrame:CGRectMake(40, 140, 70, 20)];
[coe setTitle:@"COE" forState:UIControlStateNormal];
coe.titleLabel.font = [UIFont systemFontOfSize:10];
[coe setBackgroundColor:testColor];
[coe addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[coe setTag:5];
[self.view addSubview:coe];
UIButton *changyu = [[UIButton alloc]initWithFrame:CGRectMake(120, 140, 70, 20)];
[changyu setTitle:@"长宇快递" forState:UIControlStateNormal];
changyu.titleLabel.font = [UIFont systemFontOfSize:10];
[changyu setBackgroundColor:testColor];
[changyu addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[changyu setTag:6];
[self.view addSubview:changyu];
UILabel *Y = [[UILabel alloc]initWithFrame:CGRectMake(20, 170, 10, 20)];
[Y setText:@"Y"];
[Y setFont:[UIFont systemFontOfSize:10]];
[Y setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:Y];
UIButton *yafeng = [[UIButton alloc]initWithFrame:CGRectMake(40, 170, 50, 20)];
[yafeng setTitle:@"亚风快递" forState:UIControlStateNormal];
yafeng.titleLabel.font = [UIFont systemFontOfSize:10];
[yafeng setBackgroundColor:testColor];
[yafeng addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[yafeng setTag:7];
[self.view addSubview:yafeng];
UIButton *yousu = [[UIButton alloc]initWithFrame:CGRectMake(110, 170, 50, 20)];
[yousu setTitle:@"优速快递" forState:UIControlStateNormal];
yousu.titleLabel.font = [UIFont systemFontOfSize:10];
[yousu setBackgroundColor:testColor];
[yousu addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[yousu setTag:8];
[self.view addSubview:yousu];
UIButton *other = [[UIButton alloc]initWithFrame:CGRectMake(30, 380, 300, 60)];
[other setTitle:@"其他快递" forState:UIControlStateNormal];
[other setBackgroundColor:testColor];
[other addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[other setTag:100];
[self.view addSubview:other];
}
- (void)click:(UIButton *)button
{
SecondViewController *viewScend = [[SecondViewController alloc] init];
viewScend.title = @"查询";
switch ([button tag]) {
case 1:
[viewScend companyName:@"anxinda" withChinese:@"安信达快递"];
break;
case 2:
[viewScend companyName:@"anjie" withChinese:@"安捷快递"];
break;
case 3:
[viewScend companyName:@"biaoji" withChinese:@"彪记快递"];
break;
case 4:
[viewScend companyName:@"bnt" withChinese:@"BNT"];
break;
case 5:
[viewScend companyName:@"coe" withChinese:@"COE"];
break;
case 6:
[viewScend companyName:@"changyu" withChinese:@"长宇快递"];
break;
case 7:
[viewScend companyName:@"yafeng" withChinese:@"亚风快递"];
break;
case 8:
[viewScend companyName:@"yousu" withChinese:@"优速快递"];
break;
case 100:
[viewScend companyName:@"" withChinese:@""];
break;
}
[self.navigationController pushViewController:viewScend animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end