
前言
- 上周完成了学生管理系统之后继续开始了暑期任务的末尾- 天气预报
- 天气预报的开始是要打好网络请求的基础,注意请求的位置和View刷新的时机,这些都是一些小的细节但是都是需要很注意的。
稳定的API很重要
- 网络请求得先找好一个稳定的API,对于不一样的API我们用到的数组存的东西也是不一样的
-
- 例如这个API,data是一个数组,数组里面包着字典,字典里面又存在数组,这些数组和字典都是需要在view.h文件里面提前定义好的,如果贸然换网址,可能数组或者字典的名字顺序和存放东西的位置都是会变的
推荐免费的API
- 【链接】免费天气API接口|天气预报接口|全球天气API接口-免费天气API
网络请求
- 天气预报数据讲究的是实时更新,所以我们不能去刻板的设定温度,而是要去从网上请求数据**-网络请求**
创建网络请求的步骤
- 创建请求地址
- 创建请求类
- 创建会话
- 根据会话创建任务
- 启动任务
以城市温度请求为例
- (void)creatUrl {
NSString *urlString = [NSString stringWithFormat:@"https://v0.yiketianqi.com/api?unescape=1&version=v9&appid=27289148&appsecret=7HyJN1Ny&city=%@", _cityString];
//处理字符
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//创建URL
NSURL *url = [NSURL URLWithString:urlString];
NSURLSession *session1 = [NSURLSession sharedSession];
NSURLSessionTask *task1 = [session1 dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
//解析数据
// NSInteger num = 0;
NSDictionary *secondDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSMutableArray *timeArray = [[NSMutableArray alloc] init];
timeArray = secondDictionary[@"data"];
self->_cityD = timeArray[0];
self->_city = timeArray;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self->_tableView reloadData];
}];
}
}];
[task1 resume];
}
- 解析数据的解释
NSDictionary *secondDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
-
- 我用字典把所有的内容存下来,请求Data存储
timeArray = secondDictionary[@"data"];
-
- 这里的一步就是涉及到了对于API里面数据的分析了,看如何存放,自己来决定
- 之后根据内容创建自定义的Cell然后把数组的内容存放到cell里即可
效果图
- 点击对应的城市跳转
- 可以滑动,scrollview随着城市数量的变化而变化
通知传值和属性传值的应用
对于城市数量以及重复添加的判错我用到了属性传值和通知传值,
跨多个界面-通知传值
- 刚进去的界面是城市管理界面,接着就是城市搜索界面,接着就是城市添加界面,刚开始尝试了协议传值发现数据更新的话传过来不固定,所以我在添加城市的界面发出通知,在城市界面用一个数组来接受通知,里面存着已经添加的城市,而且对于城市管理界面也有很大的帮助
- 城市添加界面,接收到层层属性传值传来的数组判断是否重复,不重复即可发出通知
- (void)pressSure {
int bool1 = 1;
NSLog(@"确定");
for (int i = 0; i < _returnArray.count; i ++) {
if ([_cityString isEqualToString:_returnArray[i]] ) {
bool1 = 0;
break;
}
}
if (bool1 == 0) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"警告" message:@"该城市添加过了" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:sure];
[self presentViewController:alertController animated:YES completion:nil];
} else {
// 发送通知并回传数据
// 注册通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"inform" object:nil userInfo:_returnD];
[self dismissViewControllerAnimated:YES completion:nil];
}
// _returnArray = [NSMutableArray arrayWithObject:_cityString];
// [_delegate city1:_returnArray :num];
// [self dismissViewControllerAnimated:YES completion:nil];
}
- 城市管理界面,可变数组接受传过来的城市名字,(通知传值传过来的是一个字典,每次都是一个key And Value)
- ViewDidLoad
// 注册观察者,用于接受界面二的通知,接收通知的名称必须和发送通知的名称保持一致才能收到
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNoticed:) name:@"inform" object:nil];
- 函数
// 观察者接受函数,
// 这里接受传值并赋值给当前页面的label
- (void)receiveNoticed:(NSNotification*)sender {
NSString* str = sender.userInfo[@"key1"];
[_cityArray addObject:str];
NSLog(@"newCitycount = %ld", _cityArray.count);
[_tableView reloadData];
}
// 移除通知
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- 记得移除通知,完成后数组里面存放了已经添加的城市。
总结
- 暑期学习了不少东西也写了不少demo,临近放假,还是要多复习多看,暑假带给我最大的感受就是多看多写多尝试。