自用天气预报查询(未来7天)
首先确定需要的界面,这里写的是小编初步设定的一个简单界面的实现。
先在AppDelegate.m中初始化VIewController的对象,具体代码如下:
ViewController *v = [[ViewController alloc]init];
UINavigationController *vNav = [[UINavigationController alloc]initWithRootViewController:v];
self.window.rootViewController = vNav;
return YES;
}
在ViewController写入如下代码:
#import "ViewController.h"
#import "WeatherViewController.h"
@interface ViewController ()<UIAlertViewDelegate>{
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 = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请您输入城市名称" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}else{
WeatherViewController *weather = [[WeatherViewController alloc]init];
weather.CityName = textF.text;
[self.navigationController pushViewController:weather animated:YES];
}
}
然后在第二个界面声明属性存放城市名称
@property(nonatomic , strong)NSString *CityName;
在.m中写入如下代码实现天气查询:
#define WEATHER_URL @"https://www.sojson.com/open/api/weather/json.shtml?city="
#import "WeatherViewController.h"
@interface WeatherViewController ()<UITableViewDelegate,UITableViewDataSource>{
UITableView *_tableV;
NSDictionary *_dic;
NSMutableDictionary *_dataDic;
NSDictionary *dic;
NSMutableArray *forecastArr;
}
@end
@implementation WeatherViewController
- (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 = [NSString stringWithFormat:@"%@%@",WEATHER_URL,ss];
// NSLog(@"%@",textUrl);
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionTask *task = [session dataTaskWithURL:[NSURL URLWithString:textUrl] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//系统自带的解析方式
self->_dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// NSLog(@"----==%@",_dic);
}];
[task resume];
[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) {
return 1;
}else{
return 6;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *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 = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
textV.font = [UIFont systemFontOfSize: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.contentView addSubview:textV];
}else if (indexPath.section == 1){
if (indexPath.row == 0) {
dic = [_dataDic objectForKey:@"yesterday"];
_tableV.rowHeight = 300;
UITextView *textV = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
textV.font = [UIFont systemFontOfSize: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.contentView addSubview:textV];
}else{
NSLog(@"num==-=-=%ld",forecastArr.count);
dic = forecastArr[indexPath.row-1];
_tableV.rowHeight = 300;
UITextView *textV = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
textV.font = [UIFont systemFontOfSize: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.contentView addSubview:textV];
}
}
return cell;
}
如图:
即可成功!