实现的效果如下所示:
代码如下:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIPickerView *cityPickView;
- (IBAction)request:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *city;
@property (weak, nonatomic) IBOutlet UILabel *temperature;
@property (weak, nonatomic) IBOutlet UILabel *windyDirection;
@property (weak, nonatomic) IBOutlet UILabel *level;
@property (weak, nonatomic) IBOutlet UILabel *moisture;
@property (weak, nonatomic) IBOutlet UILabel *time;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong)NSDictionary *dic;
@property (weak, nonatomic) IBOutlet UIView *activityView;
//@property (nonatomic,strong)UIActivityIndicatorView *activity;
//存放所有的城市名字
@property (nonatomic,strong)NSMutableArray *data;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//先隐藏
_activityView.hidden = YES;
}
//懒加载
- (NSDictionary *)dic
{
if (_dic == nil) {
NSString *path = [[NSBundle mainBundle]pathForResource:@"cityCode.plist" ofType:nil];
_dic = [NSDictionary dictionaryWithContentsOfFile:path];
}
return _dic;
}
- (NSMutableArray *)data
{
if (_data == nil) {
_data = [NSMutableArray array];
}
return _data;
}
//列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
//每列的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.dic.count;
}
//显示标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//取得所有的key
NSArray *cities = [_dic allKeys];
//_data保存所有的城市
_data = [cities copy];
//取得城市的名字
NSString *cityName = cities[row];
return cityName;
}
//当前选中的行数
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(@"row = %ld",row);
}
//请求网络数据
- (IBAction)request:(id)sender {
_activityView.hidden = NO;
//得到_cityPickView当前选中的行数
NSInteger rowIndex = [_cityPickView selectedRowInComponent:0];
//得到 选中行的城市名字
NSString *city = _data[rowIndex];
//取得城市对应的ID
NSString *cityId = _dic[city];
//构造URL对象
NSString *str = [NSString stringWithFormat:@"http://www.weather.com.cn/adat/sk/%@.html",cityId];
NSURL *url = [NSURL URLWithString:str];
//构造request对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//构造NSURLSession
NSURLSession *session= [NSURLSession sharedSession];
//创建网络任务
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//解析JSON数据
NSDictionary *dic1 = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//回到主线程 刷新UI
dispatch_async(dispatch_get_main_queue(), ^{
[self refreshUI:dic1];
});
}];
//发起网络任务
[task resume];
}
//刷新UI
- (void)refreshUI:(NSDictionary *)dic
{
_activityView.hidden = YES;
//取得数据
NSDictionary *dic2 = dic[@"weatherinfo"];
NSString *ws = dic2[@"WS"]; //风级
NSString *city = dic2[@"city"]; //城市
NSString *temp = dic2[@"temp"]; //温度
NSString *SD = dic2[@"SD"]; //湿度
NSString *Wd = dic2[@"WD"]; //风向
NSString *time = dic2[@"time"]; //时间
_city.text = city;
_temperature.text = temp;
_windyDirection.text = Wd;
_level.text = ws;
_moisture.text = SD;
_time.text = time;
}
在以后的学习中,会一直坚持写博客,记录自己的学习笔记,为自己的学习和工作做好准备!!!