显示天气预报的Demo
实现的效果如下所示:
代码如下:
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;
}
在以后的学习中,会一直坚持写博客,记录自己的学习笔记,为自己的学习和工作做好准备!!!
显示天气预报的Demo的更多相关文章
- libcurl开源库在Win7 + VS2012环境下编译、配置详解 以及下载文件并显示下载进度 demo(转载)
转载:http://blog.csdn.net/fengshuiyue/article/details/39530093(基本教程) 转载:https://my.oschina.net/u/14207 ...
- springboot1.5.9整合websocket实现实时显示的小demo
最近由于项目需要实时显示数据库更新的数据变化情况,一开始想过在前端使用ajax异步轮询方法实现,但后面考虑到性能和流量等要求,就放弃该方法而选择使用websocket(毕竟现在springboot整合 ...
- Android 拍照或相册选择照片进行显示缩放位图 Demo
拍照后直接使用 BitmapFactory.decodeStream(...) 进行创建 Bitmap 并显示是有问题的. Bitmap 是个简单对象,它只存储实际像素数据,也就是说,即使原始照片已压 ...
- 显示本月日历demo
import java.text.DateFormatSymbols; import java.util.Calendar; import java.util.GregorianCalendar; p ...
- 关于一个隐藏和显示物品列表的demo
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- Android 获取天气预报
界面布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...
- 在android中用跑马灯的效果显示textview
大家好,在我们通常的android project中,通常需要用到textview这一个布局文件,并且对于这一个显示布局所需要的文本文字内容. 下面我们就来介绍一种方法来实现在android中用跑马灯 ...
- CSS2-3常见的demo列子总结
CSS2-3常见的demo列子总结 阅读目录 1. css超过一行或者多行后显示省略号. 2. css图片未知高度垂直居中完美解决方案. 3. 学习使用 :before和 :after伪元素 回到顶部 ...
- 【AS3 Coder】任务七:初涉PureMVC——天气预报功能实现
转自:http://www.iamsevent.com/post/36.html AS3 Coder]任务七:初涉PureMVC——天气预报功能实现 使用框架:AS3任务描述:了解PureMVC框架使 ...
随机推荐
- SQL Server获取月度列表
-- 获取月度列表 if exists(select 1 from sysobjects where name = 'proc_GetDateMonthList' and type = 'p') dr ...
- .NET牛人应该知道些什么
任何一个使用.NET的人 1.描述线程与进程的区别? 线程(Thread)与进程(Process)二者都定义了某种边界,不同的是进程定义的是应用程序与应用程序之间的边界,不同的进程之间不能共享代 码和 ...
- Hibernate注解使用以及Spring整合
Hibernate注解使用以及Spring整合 原文转自:http://wanqiufeng.blog.51cto.com/409430/484739 (1) 简介: 在过去几年里,Hibernate ...
- android嵌套unity3d
最近因为跟小伙伴在制作一个App参加比赛,由于有unity的开发经验,突发奇想的想要在Android应用中内嵌unity提供模型展示的功能. 为此,我们查阅了不少资料.大多发现的是unity中内嵌An ...
- JS 中的事件绑定、事件监听、事件委托
事件绑定 要想让 JavaScript 对用户的操作作出响应,首先要对 DOM 元素绑定事件处理函数.所谓事件处理函数,就是处理用户操作的函数,不同的操作对应不同的名称. 在JavaScript中,有 ...
- Matlab中的一些小技巧
(转于它处,仅供参考) 1.. Ctrl+C 中断正在执行的操作 如果程序不小心进入死循环,或者计算时间太长,可以在命令窗口中使用Ctrl+c来中断.MATLAB这时可能正疲于应付,响应会有些滞后. ...
- nice-validator验证插件
主要是作为form表单的验证,密码,确认密码的验证,远程验证的功能: 1:先导包:nice-validator 2:引入文件css,js 3: 使用 使用文档:http://www.niceue.co ...
- python types模块
types模块成员: ['BooleanType', 'BufferType', 'BuiltinFunctionType', 'BuiltinMethodType', 'ClassType', 'C ...
- Redis配置文件参数说明
Redis配置文件参数说明 1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize no 2. 当Redis以守护进程方式运行时,Redis ...
- Winform 窗体单例
有窗体Form1和窗体Form2,单击Form1上按钮,只弹出一个Form2. Form2里自定义一个方法,里面判断是否弹出Form2,没有时弹出Form2. public static Form2 ...