2018-07-01粑粑

LoadData.h里

// 創(chuàng)建單利方法

+(instancetype)showCityMessage;

// 解析城市天氣預(yù)報方法

-(void)getCityWeather:(NSString*)url;



LoadData.m里

@interface LoadData(){

? ? // 創(chuàng)建可變數(shù)組

? ? NSMutableDictionary*_cityWeatherDic;

}

@end

// 創(chuàng)建單利變量

static LoadData *ld;

@implementation LoadData

// 實現(xiàn)創(chuàng)建單利方法

+(instancetype)showCityMessage{

? ? // 防止重復初始化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];

}

@end

ViewController.m里

#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


WeatherViewController.h里

#import

@interfaceWeatherViewController :UIViewController

@property (nonatomic,strong)NSMutableDictionary *messageDic;

@end


WeatherViewController.m里

拖控件

@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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容