使用MVC模式
創(chuàng)建LodData繼承NSObject
LodData.h中創(chuàng)建單例模式、解析天氣
// 創(chuàng)建單利方法
+(instancetype)showCityMessage;
// 解析城市天氣預(yù)報方法
-(void)getCityWeather:(NSString*)url;
LodData.m
@interface LoadData()
{
? ? // 創(chuàng)建可變數(shù)組
? ? NSMutableDictionary*_cityWeatherDic;
}
@end
// 創(chuàng)建單利變量
static LoadData *ld;
@implementation LoadData
// 實現(xiàn)創(chuàng)建單利方法
+(instancetype)showCityMessage{
? ? // 防止重復(fù)初始化Ld變量
? ? staticdispatch_once_tonceToken;
? ? dispatch_once(&onceToken, ^{
? ? ? ? ld= [[LoadDataalloc]init];
? ? });
? ? return ld;
}
+(instancetype)allocWithZone:(struct_NSZone*)zone{
? ? if(!ld) {
? ? ? ? ld= [superallocWithZone:zone];
? ? }
? ? return ld;
}
- (id)copy{
? ? return self;
}
- (id)mutableCopy{
? ? return self;
}
// 實現(xiàn)解析城市天氣預(yù)報方法
-(void)getCityWeather:(NSString*)url{
? ? NSLog(@"%@",url);
? ? // 創(chuàng)建URl解析
? ? NSURLSession *session = [NSURLSession sharedSession];
? ? _cityWeatherDic = [NSMutableDictionary dictionary];
? ? // 創(chuàng)建task
? ? NSURLSessionTask*task = [sessiondataTaskWithURL:[NSURLURLWithString:url]completionHandler:^(NSData*_Nullabledata,NSURLResponse*_Nullableresponse,NSError*_Nullableerror) {
? ? ? ? self->_cityWeatherDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//? ? ? ? NSLog(@"數(shù)據(jù)==%@",self->_cityWeatherDic);
? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? [[NSNotificationCenter defaultCenter]postNotificationName:@"getCityWeather" object:self->_cityWeatherDic];
? ? ? ? });
? ? }];
? ? // 開始請求
? ? [taskresume];
}
#import "ViewController.h"
#import "LoadData.h"
#import "WeatherViewController.h"
// 創(chuàng)建接口
#define WEATHER_URL @"https://www.sojson.com/open/api/weather/json.shtml?city=%@"
@interface ViewController (){
? ? // 創(chuàng)建對象
? ? LoadData*ld;
? ? WeatherViewController *weatherVc;
}
@property (strong, nonatomic) IBOutlet UILabel *cityLb;
@property (strong, nonatomic) IBOutlet UITextField *cityTF;
// 創(chuàng)建可變數(shù)組接收數(shù)據(jù)
//@property (nonatomic,strong)NSMutableDictionary *weatherDic;
@end
@implementation ViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? // 注冊通知
? ? [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getCityWeather:) name:@"getCityWeather" object:nil];
?? ? // 初始化ld
? ? ld = [LoadData showCityMessage];
}
#pragma mark - 注冊通知接收數(shù)據(jù)方法
- (void)getCityWeather:(NSNotification*)notifi{
? ? //? ? // 初始化對象
? ? weatherVc = [[WeatherViewController alloc]init];
? ? weatherVc.messageDic = notifi.object;
//? ? NSLog(@"===%@",weatherVc.messageDic);
? ? // 跳轉(zhuǎn)
? ? [self presentViewController:weatherVc animated:YES completion:nil];
}
// 點擊按鈕方法
- (IBAction)cityBtn:(id)sender {
? ? NSString*data =self.cityTF.text;
? ? //轉(zhuǎn)換成UTF-8
? ? NSString *dataUTF8 = [data stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
? ? NSString*str = [NSStringstringWithFormat:WEATHER_URL,dataUTF8];
? ? [ld getCityWeather:str];
}
@end
@interfaceWeatherViewController :UIViewController
@property (nonatomic,strong)NSMutableDictionary *messageDic;
@end
#import "WeatherViewController.h"
@interface WeatherViewController ()
@property (strong, nonatomic) IBOutlet UILabel *cityLb;
@property (strong, nonatomic) IBOutlet UILabel *shiduLb;
@property (strong, nonatomic) IBOutlet UILabel *pm25Lb;
@property (strong, nonatomic) IBOutlet UILabel *pm10Lb;
@property (strong, nonatomic) IBOutlet UILabel *wenduLb;
@property (strong, nonatomic) IBOutlet UITextView *ganmaoLb;
@end
@implementationWeatherViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? // 進行賦值
? ? self.cityLb.text = self.messageDic[@"city"];
? ? NSMutableDictionary*otherDic =self.messageDic[@"data"];
? ? self.shiduLb.text= otherDic[@"shidu"];
? ? self.wenduLb.text= otherDic[@"wendu"];
? ? self.pm25Lb.text = self.messageDic[@"date"];
? ? self.pm10Lb.text= otherDic[@"quality"];
? ? self.wenduLb.text= otherDic[@"wendu"];
? ? self.ganmaoLb.text= otherDic[@"ganmao"];
}
- (void)didReceiveMemoryWarning {
? ? [super didReceiveMemoryWarning];
? ? // Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
? ? [self dismissViewControllerAnimated:YES completion:nil];
}
@end