#import "ViewController.h"
#import "WeatherViewController.h"
@interface ViewController (){
UITextField*textF;
UIButton*queryBtn;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"查询城市天气";
textF = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.size.width*0.15, self.view.frame.size.width*0.3, self.view.frame.size.width*0.7, self.view.frame.size.height*0.08)];
textF.borderStyle = UITextBorderStyleRoundedRect;
textF.placeholder = @"请输入您要查询的城市名称";
[self.view addSubview:textF];
queryBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width*0.4, self.view.frame.size.width*0.5, self.view.frame.size.width*0.2, self.view.frame.size.height*0.05)];
[queryBtn setTitle:@"查询" forState:UIControlStateNormal];
[queryBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
queryBtn.backgroundColor = [UIColor cyanColor];
[queryBtn addTarget:self action:@selector(queryInformation) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:queryBtn];
}
-(void)queryInformation{
if(textF.text==nil) {
UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"请您输入城市名称"delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil];
[alertshow];
}else{
WeatherViewController *weather = [[WeatherViewController alloc]init];
weather.CityName=textF.text;
[self.navigationController pushViewController:weather animated:YES];
}}
创建UIViewController‘
在.h中注册
@property(nonatomic , strong)NSString *CityName;
在.m中
#define WEATHER_URL @"https://www.sojson.com/open/api/weather/json.shtml?city="
#import "WeatherViewController.h"
@interface WeatherViewController (){
UITableView*_tableV;
NSDictionary *_dic;
NSMutableDictionary *_dataDic;
NSDictionary *dic;
NSMutableArray*forecastArr;
}
@end
@implementationWeatherViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = self.CityName;
self.view.backgroundColor = [UIColor whiteColor];
_dic = [NSDictionary dictionary];
dic = [NSDictionary dictionary];
_dataDic = [NSMutableDictionary dictionary];
forecastArr = [NSMutableArray array];
NSString *ss = [self.CityName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString*textUrl = [NSStringstringWithFormat:@"%@%@",WEATHER_URL,ss];
// NSLog(@"%@",textUrl);
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionTask*task = [sessiondataTaskWithURL:[NSURLURLWithString:textUrl]completionHandler:^(NSData*_Nullabledata,NSURLResponse*_Nullableresponse,NSError*_Nullableerror) {
//系统自带的解析方式
self->_dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// NSLog(@"----==%@",_dic);
}];
[taskresume];
[self tableViewAddToView];
}
-(void)tableViewAddToView{
_tableV = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableV.delegate=self;
_tableV.dataSource = self;
[self.view addSubview:_tableV];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
return 2;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
if(section ==0) {
return1;
}else{
return6;
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
staticNSString*str =@"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
if(!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
_dataDic = [_dic objectForKey:@"data"];
forecastArr = [_dataDic objectForKey:@"forecast"];
if(indexPath.section==0) {
_tableV.rowHeight=300;
UITextView*textV = [[UITextViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,300)];
textV.font= [UIFontsystemFontOfSize:25];
textV.text = [NSString stringWithFormat:@" 湿度:%@ \n pm2.5:%@ \n pm1.0:%@ \n 空气质量:%@ \n 温度:%@ \n 健康:%@",[_dataDic objectForKey:@"shidu"],[_dataDic objectForKey:@"pm25"],[_dataDic objectForKey:@"pm10"],[_dataDic objectForKey:@"quality"],[_dataDic objectForKey:@"wendu"],[_dataDic objectForKey:@"ganmao"]];
// textV.userInteractionEnabled = NO;
[cell.contentViewaddSubview:textV];
}elseif(indexPath.section==1){
if(indexPath.row==0) {
dic= [_dataDicobjectForKey:@"yesterday"];
_tableV.rowHeight=300;
UITextView*textV = [[UITextViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,300)];
textV.font= [UIFontsystemFontOfSize:20];
textV.text = [NSString stringWithFormat:@" 日期:%@ \n 日出时间:%@ \n 最高温度:%@ \n 最低温度:%@ \n 日落时间:%@ \n 空气质量:%@ \n 风向:%@ \n 风级:%@ \n 天气状态:%@ \n 注意:%@ ",[dic objectForKey:@"date"],[dic objectForKey:@"sunrise"],[dic objectForKey:@"high"],[dic objectForKey:@"low"],[dic objectForKey:@"sunset"],[dic objectForKey:@"aqi"],[dic objectForKey:@"fx"],[dic objectForKey:@"fl"],[dic objectForKey:@"type"],[dic objectForKey:@"notice"]];
// textV.userInteractionEnabled = NO;
[cell.contentViewaddSubview:textV];
}else{
NSLog(@"num==-=-=%ld",forecastArr.count);
dic=forecastArr[indexPath.row-1];
_tableV.rowHeight=300;
UITextView*textV = [[UITextViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,300)];
textV.font= [UIFontsystemFontOfSize:20];
textV.text = [NSString stringWithFormat:@" 日期:%@ \n 日出时间:%@ \n 最高温度:%@ \n 最低温度:%@ \n 日落时间:%@ \n 空气质量:%@ \n 风向:%@ \n 风级:%@ \n 天气状态:%@ \n 注意:%@ ",[dic objectForKey:@"date"],[dic objectForKey:@"sunrise"],[dic objectForKey:@"high"],[dic objectForKey:@"low"],[dic objectForKey:@"sunset"],[dic objectForKey:@"aqi"],[dic objectForKey:@"fx"],[dic objectForKey:@"fl"],[dic objectForKey:@"type"],[dic objectForKey:@"notice"]];
// textV.userInteractionEnabled = NO;
[cell.contentViewaddSubview:textV];
}
}
returncell;
}
网友评论