背景:继续破解打卡,用IOS开发出一个APP.单个helloWorldOK了,和后端互动成功,现在就要将解析出的Json的结果放在页面上。同时期望按钮点击后,能改变页面上某些Label的值。
环境:Mac OS 10.11.5+Xcode7.3.1
1、IOS自从5以后就自带了解析了Json的功能,有一个类叫NSJSONSerialization,参考教程
如果自己懒得写后台Json的话,可以用一些公共的Json来练习。
2、按钮点击的事件,可以通过IBAction来绑定。具体操作就是在Story面板上,选择某个按钮,右键后会出现一堆事件列表,选择一个,点加号,然后拖到ViewController.m中,这样就会建立一个链接了。
3、关于事件点击后,如何控制Label的值。这个要用Outlet来解决。思路和2非常像,不过在拖动之前,需要先在ViewController.h和ViewController.m中分别新建Property和thesize.下面的动作就和2差不多了。在某个按钮上右击,找到.h中的Outlet,拖动即可。
4、控制页面跳转教程(仍未测试)
总结:IOS基础非常不扎实,导致好多地方走了非常多的弯路,仍然没走过去。
主要体现在:Json出来的数据判断报错或者无效。给Story上的控件赋值报错
下面附上我Controller的源码,写的较挫,回头还要再优化。
// // ViewController.m // dakaDemo // // Created by gemmy on 16/7/24. // Copyright © 2016年 shundepg. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize labelDate; @synthesize initBtn; @synthesize amBtn; @synthesize pmBtn; @synthesize labelAmId; @synthesize labelPmId; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)initClick:(id)sender { NSError *error; //1.确定请求路径 //NSURL *url = [NSURL URLWithString:@"http://apistore.baidu.com/microservice/weather?cityid=101010100"]; NSURL *url = [NSURL URLWithString:@"http://115.182.83.55:8003/mobile"]; //2.创建可变的请求对象 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //3.修改请求方法为POST(大写) request.HTTPMethod = @"POST"; //3.1 人为构造服务器需要的json格式 NSDictionary * dic= @{@"methodName":@"getMyAttendance",@"orderNo":@"1463358870034",@"version":@"1.1.0",@"needReLogin":@"false",@"osType":@"IOS",@"deviceToken":@"",@"IOSUuid":@"201605160834302541341878187",@"mac":@"201605160834302541341878187",@"token":@"1b38dfa19a056fdb101543c5a6f6cdde",@"ip":@"",@"email":@"[email protected]",@"userName":@"张大吉",@"companyLogoUrl":@"http://www.xxxtd.com/pc/images/icon_logo.png",@"phone":@"18812345678"}; NSLog(@"DIC = %@",dic); NSData * data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error]; // 把一个json格式的data,解析为OC中的NSString类型对象 // 无特殊意义,一般用来查看JSON文本封装成 数组 还是 对象(OC中为字典) NSString * jsonString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"-----------------------------%@",jsonString); NSString *dataStr=[NSString stringWithFormat:@"requestJson=%@",jsonString]; //4.设置请求体 request.HTTPBody = [dataStr dataUsingEncoding:NSUTF8StringEncoding]; //设置请求头信息 [request setValue:@"ios 9.0" forHTTPHeaderField:@"User-Agent"]; //设置请求超时 request.timeoutInterval = 60; //将请求的url数据放到NSData对象中 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中 //这里是完整的Json串 NSDictionary *fullDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; NSArray *objectArray = [fullDic objectForKey:@"object"]; NSLog(@"objectArray:%@",objectArray); //遍历输出object里的list for(NSDictionary *aObject in objectArray){ NSArray *attendArray = [aObject objectForKey:@"list"]; NSInteger count=0; for(NSDictionary *aAttend in attendArray){ NSString *id = [aAttend objectForKey:@"id"]; NSString *isClock = [aAttend objectForKey:@"isClock"]; NSString *date = [aAttend objectForKey:@"date"]; if(count == 0){ //1.分别给控件设置状态 //1.1 日期设置好 // labelDate.text=date; //1.2 设置上班打卡状态 // if([isClock isEqualToString:@"1"]){ // [amBtn setTitle:@"已打卡" forState:UIControlStateNormal]; // }else if([isClock isEqualToString:@"0"]){ // [amBtn setTitle:@"未打卡" forState:UIControlStateNormal]; // } // [amBtn setTitle:isClock forState:UIControlStateNormal]; //1.3 设置id labelAmId.text=id; NSLog(@"id:%@",id); }else if (count ==1 ){ //2.分别给控件设置状态 //2.2 设置下班打卡状态 // if([isClock isEqualToString:@"1"]){ // [amBtn setTitle:@"已打卡" forState:UIControlStateNormal]; // }else if([isClock isEqualToString:@"0"]){ // [amBtn setTitle:@"未打卡" forState:UIControlStateNormal]; // } //1.3 设置id labelPmId.text=id; NSLog(@"id:%@",id); } count++; } } NSLog(@"attendency字典里面的内容为--》%@", fullDic ); //1.点击此按钮后,将未打卡的状态全部置成已打卡 //1.1 设置日期 // labelDate.text=[weatherInfo objectForKey:@"city"];//1 //1.2 设置上班打卡状态 // [amBtn setTitle:[weatherInfo objectForKey:@"city"] forState:UIControlStateNormal]; //1.3 设置下班打卡状态 // [pmBtn setTitle:[weatherInfo objectForKey:@"city"] forState:UIControlStateNormal]; } - (IBAction)amBtnClick:(id)sender { //1.确定请求路径 NSURL *url = [NSURL URLWithString:@"http://apistore.baidu.com/microservice/weather?cityid=101010100"]; //2.创建可变的请求对象 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //3.修改请求方法为POST(大写) request.HTTPMethod = @"POST"; //4.设置请求体 //request.HTTPBody = [@"username=dede&pwd=ede&type=JSON" dataUsingEncoding:NSUTF8StringEncoding]; //设置请求头信息 [request setValue:@"ios 9.0" forHTTPHeaderField:@"User-Agent"]; //设置请求超时 request.timeoutInterval = 10; NSError *error; //将请求的url数据放到NSData对象中 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中 NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; NSDictionary *weatherInfo = [weatherDic objectForKey:@"retData"]; NSString *str = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]]; NSLog(@"weatherInfo字典里面的内容为--》%@", weatherDic ); NSLog(@"%@", str ); //1.2 设置上班打卡状态 [amBtn setTitle:[weatherInfo objectForKey:@"city"] forState:UIControlStateNormal]; NSLog(@"amBtn..."); } - (IBAction)pmBtnClick:(id)sender { NSError *error; //加载一个NSURL对象 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://apistore.baidu.com/microservice/weather?cityid=101010100"]]; //将请求的url数据放到NSData对象中 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中 NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; NSDictionary *weatherInfo = [weatherDic objectForKey:@"retData"]; NSString *str = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]]; NSLog(@"weatherInfo字典里面的内容为--》%@", weatherDic ); NSLog(@"%@", str ); //1.3 设置下班打卡状态 [pmBtn setTitle:[weatherInfo objectForKey:@"city"] forState:UIControlStateNormal]; NSLog(@"amBtn..."); } @end