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{

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

}

@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)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市槐秧,隨后出現(xiàn)的幾起案子啄踊,更是在濱河造成了極大的恐慌,老刑警劉巖色鸳,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件社痛,死亡現(xiàn)場離奇詭異,居然都是意外死亡命雀,警方通過查閱死者的電腦和手機蒜哀,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來吏砂,“玉大人撵儿,你說我怎么就攤上這事『” “怎么了淀歇?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長匈织。 經(jīng)常有香客問我浪默,道長,這世上最難降的妖魔是什么缀匕? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任纳决,我火速辦了婚禮,結(jié)果婚禮上乡小,老公的妹妹穿的比我還像新娘阔加。我一直安慰自己,他們只是感情好满钟,可當(dāng)我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布胜榔。 她就那樣靜靜地躺著胳喷,像睡著了一般。 火紅的嫁衣襯著肌膚如雪夭织。 梳的紋絲不亂的頭發(fā)上吭露,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天,我揣著相機與錄音摔癣,去河邊找鬼奴饮。 笑死,一個胖子當(dāng)著我的面吹牛择浊,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播逾条,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼琢岩,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了师脂?” 一聲冷哼從身側(cè)響起担孔,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎吃警,沒想到半個月后糕篇,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡酌心,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年拌消,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片安券。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡墩崩,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出侯勉,到底是詐尸還是另有隱情鹦筹,我是刑警寧澤,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布址貌,位于F島的核電站铐拐,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏练对。R本人自食惡果不足惜遍蟋,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望锹淌。 院中可真熱鬧匿值,春花似錦、人聲如沸赂摆。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至绊谭,卻和暖如春政恍,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背达传。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工篙耗, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人宪赶。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓宗弯,卻偏偏與公主長得像,于是被迫代替她去往敵國和親搂妻。 傳聞我的和親對象是個殘疾皇子蒙保,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內(nèi)容