标签:json ui 天气预报 网络加载 uinavigationcontroll
FirstViewController.m文件
#import "FirstViewController.h"
#import "JRViewController.h"
#import "CityModel.h"
#import "JSONKit.h"
#define kW self.view.frame.size.width
#define kH self.view.frame.size.height
@interface FirstViewController ()
//当前城市
@property (nonatomic,copy) NSString * cityName;
//当前日期
@property (nonatomic,strong) NSArray * dateArray;
//当前天气小图片
@property (nonatomic,strong) NSArray * imageViewArray;
//当前温度label
@property (nonatomic,strong) NSArray * temperArray;
//天气详情介绍
@property (nonatomic,strong) NSArray * introArray;
//右上角,当前温度
@property (nonatomic,copy) NSString * rightTemper;
//右上角,天气情况
@property (nonatomic,copy) NSString * rightIntro;
//晒衣指数
@property (nonatomic,copy) NSString * dress;
//紫外线指数
@property (nonatomic,copy) NSString * purple;
//洗车指数
@property (nonatomic,copy) NSString * car;
//人体舒适程度
@property (nonatomic,copy) NSString * comfort;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
UIImageView * imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"DuoYun"]];
imageView.frame=self.view.bounds;
[self.view addSubview:imageView];
self.title=@"天气预报";
//加载烟台的天气
[self _loadYT];
//加载视图
[self _loadView];
}
#pragma mark - 加载视图
- (void) _loadView
{
//右上角当天温度情况
UILabel * leftLabel=[[UILabel alloc]initWithFrame:CGRectMake(200, 60, kW-200, 50)];
//leftLabel.backgroundColor=[UIColor redColor];
leftLabel.textAlignment=NSTextAlignmentCenter;
leftLabel.text=_rightTemper;
[leftLabel setFont:[UIFont fontWithName:nil size:30]];
leftLabel.textColor=[UIColor whiteColor];
[self.view addSubview:leftLabel];
//右上角,天气情况
UILabel * rightIntroLabel=[[UILabel alloc]initWithFrame:CGRectMake(200, 100, kW-200, 40)];
//rightIntroLabel.backgroundColor=[UIColor greenColor];
rightIntroLabel.text=_rightIntro;
[rightIntroLabel setTextColor:[UIColor blueColor]];
rightIntroLabel.textAlignment=NSTextAlignmentCenter;
[self.view addSubview:rightIntroLabel];
//当前城市
UILabel * RightCityLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 70, 150, 70)];
//RightCityLabel.backgroundColor=[UIColor greenColor];
[RightCityLabel setTextColor:[UIColor whiteColor]];
RightCityLabel.textAlignment=NSTextAlignmentCenter;
RightCityLabel.text=_cityName;
[RightCityLabel setFont:[UIFont fontWithName:nil size:40]];
[self.view addSubview:RightCityLabel];
//切换城市按钮
UIButton * changeCityButton=[[UIButton alloc]initWithFrame:CGRectMake((kW-100)/2, kH-50, 100, 45)];
changeCityButton.backgroundColor=[UIColor purpleColor];
[changeCityButton setTitle:@"切换城市" forState:UIControlStateNormal];
[changeCityButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[changeCityButton addTarget:self action:@selector(changeCity) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:changeCityButton];
//下方多日天气视图
for (int i=0; i<4; i++)
{
UIView * view=[[UIView alloc]initWithFrame:CGRectMake(((kW-4*80)/5)*(i+1)+i*80, kH-290, 80, 230)];
view.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.1];
//date
UILabel * dateLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 50)];
dateLabel.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.2];
dateLabel.text=_dateArray[i];
dateLabel.textAlignment=NSTextAlignmentCenter;
[view addSubview:dateLabel];
//image
UIImageView * imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 55, 80, 80)];
//imageView.backgroundColor=[UIColor greenColor];
NSURL * url=[NSURL URLWithString:_imageViewArray[i]];
NSData * data=[NSData dataWithContentsOfURL:url];
UIImage * image=[UIImage imageWithData:data];
imageView.image=image;
[view addSubview:imageView];
//天气情况简介label
UILabel * intrLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 130, 80, 60)];
//intrLabel.backgroundColor=[UIColor redColor];
intrLabel.textAlignment=NSTextAlignmentCenter;
intrLabel.adjustsFontSizeToFitWidth=YES;
intrLabel.text=_introArray[i];
[view addSubview:intrLabel];
//温度label
UILabel * tempery=[[UILabel alloc]initWithFrame:CGRectMake(0, 190, 80, 40)];
tempery.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.2];
tempery.text=_temperArray[i];
tempery.textAlignment=NSTextAlignmentCenter;
[tempery setFont:[UIFont fontWithName:nil size:15]];
[view addSubview:tempery];
[self.view addSubview:view];
//中间比较大的视图
UIView * middleView=[[UIView alloc]initWithFrame:CGRectMake(50, 140, kW-100, 225)];
middleView.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.05];
//晒衣指数
UILabel * dressLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, 10, 100, 40)];
dressLabel.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.06];
dressLabel.text=@"晒衣指数";
dressLabel.textAlignment=NSTextAlignmentCenter;
dressLabel.textColor=[UIColor whiteColor];
[middleView addSubview:dressLabel];
UILabel * RdressLabel=[[UILabel alloc]initWithFrame:CGRectMake(180, 5, 250, 50)];
//RdressLabel.backgroundColor=[UIColor redColor];
RdressLabel.text=_dress;
[RdressLabel setTextColor:[UIColor whiteColor]];
[middleView addSubview:RdressLabel];
//紫外线指数
UILabel * purpleLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, 65, 100,40)];
purpleLabel.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.06];
purpleLabel.text=@"紫外线指数";
purpleLabel.textAlignment=NSTextAlignmentCenter;
purpleLabel.textColor=[UIColor whiteColor];
[middleView addSubview: purpleLabel];
UILabel * RpurpleLabel=[[UILabel alloc]initWithFrame:CGRectMake(180, 10+50, 250,50)];
//RpurpleLabel.backgroundColor=[UIColor redColor];
RpurpleLabel.text=_purple;
[RpurpleLabel setTextColor:[UIColor whiteColor]];
[middleView addSubview: RpurpleLabel];
//洗车指数
UILabel * carLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, 65+40+15, 100, 40)];
carLabel.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.06];
carLabel.text=@"洗车指数";
carLabel.textAlignment=NSTextAlignmentCenter;
carLabel.textColor=[UIColor whiteColor];
[middleView addSubview:carLabel];
UILabel * RcarLabel=[[UILabel alloc]initWithFrame:CGRectMake(180, 65+50, 250, 50)];
//RcarLabel.backgroundColor=[UIColor redColor];
RcarLabel.text=_car;
[RcarLabel setTextColor:[UIColor whiteColor]];
[middleView addSubview:RcarLabel];
//人体舒适程度
UILabel * personComfortLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, 115+50+5, 100, 40)];
personComfortLabel.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.06];
personComfortLabel.text=@"人体舒适程度";
personComfortLabel.adjustsFontSizeToFitWidth=YES;
personComfortLabel.textAlignment=NSTextAlignmentCenter;
personComfortLabel.textColor=[UIColor whiteColor];
[middleView addSubview:personComfortLabel];
UILabel * RpersonComfortLabel=[[UILabel alloc]initWithFrame:CGRectMake(180, 115+50, 250, 50)];
//RpersonComfortLabel.backgroundColor=[UIColor redColor];
RpersonComfortLabel.text=_comfort;
[RpersonComfortLabel setTextColor:[UIColor whiteColor]];
[middleView addSubview:RpersonComfortLabel];
[self.view addSubview:middleView];
}
}
#pragma mark - 加载烟台的天气
- (void) _loadYT
{
NSURL * url=[NSURL URLWithString:@"http://m.weather.com.cn/atad/101120501.html"];
NSData * data=[NSData dataWithContentsOfURL:url];
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSDictionary * tempDic=dic[@"weatherinfo"];
//城市名
NSString * cityName=tempDic[@"city"];
_cityName=cityName;
//日期数组
NSDate * date1=[NSDate date];
NSDate * date2=[NSDate dateWithTimeIntervalSinceNow:60*60*24];
NSDate * date3=[NSDate dateWithTimeIntervalSinceNow:60*60*24*2];
NSDate * date4=[NSDate dateWithTimeIntervalSinceNow:60*60*24*3];
NSDateFormatter * format=[[NSDateFormatter alloc]init];
[format setDateFormat:@"MM月dd日"];
NSString * str1=[format stringFromDate:date1];
NSString * str2=[format stringFromDate:date2];
NSString * str3=[format stringFromDate:date3];
NSString * str4=[format stringFromDate:date4];
_dateArray=@[str1,str2,str3,str4];
//温度数组
NSString * temper1=tempDic[@"temp1"];
NSString * temper2=tempDic[@"temp2"];
NSString * temper3=tempDic[@"temp3"];
NSString * temper4=tempDic[@"temp4"];
_temperArray=@[temper1,temper2,temper3,temper4];
//天气小图标数组---存放图片地址
NSString * string1=tempDic[@"img1"];
NSInteger index1=[string1 integerValue];
NSString * imageSrc1=@"http://i.tq121.com.cn/i/mobile/images/d%02d.png";
imageSrc1=[NSString stringWithFormat:imageSrc1,index1];
//NSLog(@"%@",imageSrc1);
NSString * string2=tempDic[@"img3"];
NSInteger index2=[string2 integerValue];
NSString * imageSrc2=@"http://i.tq121.com.cn/i/mobile/images/d%02d.png";
imageSrc2=[NSString stringWithFormat:imageSrc2,index2];
//NSLog(@"%@",imageSrc2);
NSString * string3=tempDic[@"img5"];
NSInteger index3=[string3 integerValue];
NSString * imageSrc3=@"http://i.tq121.com.cn/i/mobile/images/d%02d.png";
imageSrc3=[NSString stringWithFormat:imageSrc3,index3];
//NSLog(@"%@",imageSrc3);
NSString * string4=tempDic[@"img7"];
NSInteger index4=[string4 integerValue];
NSString * imageSrc4=@"http://i.tq121.com.cn/i/mobile/images/d%02d.png";
imageSrc4=[NSString stringWithFormat:imageSrc4,index4];
//NSLog(@"%@",imageSrc4);
_imageViewArray=@[imageSrc1,imageSrc2,imageSrc3,imageSrc4];
//天气详情数组
NSString * intr1=tempDic[@"weather1"];
NSString * intr2=tempDic[@"weather2"];
NSString * intr3=tempDic[@"weather3"];
NSString * intr4=tempDic[@"weather4"];
_introArray=@[intr1,intr2,intr3,intr4];
//右上角当天的温度情况
_rightTemper=tempDic[@"temp1"];
//右上角当天天气情况
_rightIntro=tempDic[@"weather1"];
//晾衣指数
_dress=tempDic[@"index_ls"];
//紫外线指数
_purple=tempDic[@"index_uv"];
//洗车指数
_car=tempDic[@"index_xc"];
//人体舒适程度
_comfort=tempDic[@"index_co"];
}
- (void) _loadData
{
NSString * path=[[NSBundle mainBundle] pathForResource:@"weather_city_code.json" ofType:nil];
NSData * data=[NSData dataWithContentsOfFile:path];
NSDictionary * dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//NSLog(@"%@",dic);
NSArray * array=dic[@"城市代码"];
//NSLog(@"%@",array);
//创建model
CityModel * citymodel=[[CityModel alloc]init];
for (int i=0; i<array.count; i++)
{
NSDictionary * tempDic=array[i];
//NSLog(@"%@",tempDic);
citymodel.provinceName=tempDic[@"省"]; //获取到各省
//NSLog(@"%@",citymodel.provinceName);
NSArray * cityArray=tempDic[@"市"];
for(NSDictionary * cityDic in cityArray)
{
NSLog(@"%@",cityDic);
// _cityArray=cityDic[@"市名"];
// _codeArray=cityDic[@"编码"];
}
}
}
- (void) changeCity
{
JRViewController * jrVC=[[JRViewController alloc]init];
[self.navigationController pushViewController:jrVC animated:YES];
}
@end
JSON文件
{
"weatherinfo": {
"city": "烟台",
"city_en": "yantai",
"date_y": "2015年6月15日",
"date": "",
"week": "星期一",
"fchh": "11",
"cityid": "101120501",
"temp1": "29℃~19℃",
"temp2": "31℃~19℃",
"temp3": "31℃~19℃",
"temp4": "28℃~18℃",
"temp5": "26℃~17℃",
"temp6": "26℃~17℃",
"tempF1": "84.2℉~66.2℉",
"tempF2": "87.8℉~66.2℉",
"tempF3": "87.8℉~66.2℉",
"tempF4": "82.4℉~64.4℉",
"tempF5": "78.8℉~62.6℉",
"tempF6": "78.8℉~62.6℉",
"weather1": "晴",
"weather2": "晴转多云",
"weather3": "多云",
"weather4": "多云转雷阵雨",
"weather5": "多云",
"weather6": "多云",
"img1": "0",
"img2": "99",
"img3": "0",
"img4": "1",
"img5": "1",
"img6": "99",
"img7": "1",
"img8": "4",
"img9": "1",
"img10": "99",
"img11": "1",
"img12": "99",
"img_single": "0",
"img_title1": "晴",
"img_title2": "晴",
"img_title3": "晴",
"img_title4": "多云",
"img_title5": "多云",
"img_title6": "多云",
"img_title7": "多云",
"img_title8": "雷阵雨",
"img_title9": "多云",
"img_title10": "多云",
"img_title11": "多云",
"img_title12": "多云",
"img_title_single": "晴",
"wind1": "南风3-4级",
"wind2": "南风3-4级",
"wind3": "南风3-4级",
"wind4": "东南风4-5级",
"wind5": "东南风4-5级转3-4级",
"wind6": "东北风转东风3-4级",
"fx1": "南风",
"fx2": "南风",
"fl1": "3-4级",
"fl2": "3-4级",
"fl3": "3-4级",
"fl4": "4-5级",
"fl5": "4-5级转3-4级",
"fl6": "3-4级",
"index": "热",
"index_d": "天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。",
"index48": "",
"index48_d": "",
"index_uv": "强",
"index48_uv": "",
"index_xc": "较适宜",
"index_tr": "适宜",
"index_co": "较舒适",
"st1": "28",
"st2": "18",
"st3": "30",
"st4": "17",
"st5": "30",
"st6": "17",
"index_cl": "较适宜",
"index_ls": "极适宜",
"index_ag": "不易发"
}
}
PS:该案例的关键之处在于截取JSON文件,一层一层解开,直到挑出对自己有用的部分~ (说的简单,解了一上午,把自己给绕进去了,蠢哭~~~)
标签:json ui 天气预报 网络加载 uinavigationcontroll
原文地址:http://blog.csdn.net/qq_27364431/article/details/46507493