2018-07-01

使用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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末徒溪,一起剝皮案震驚了整個濱河市忿偷,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌臊泌,老刑警劉巖鲤桥,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異渠概,居然都是意外死亡茶凳,警方通過查閱死者的電腦和手機嫂拴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來贮喧,“玉大人筒狠,你說我怎么就攤上這事∠渎伲” “怎么了辩恼?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長谓形。 經(jīng)常有香客問我灶伊,道長,這世上最難降的妖魔是什么寒跳? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任聘萨,我火速辦了婚禮,結(jié)果婚禮上童太,老公的妹妹穿的比我還像新娘米辐。我一直安慰自己,他們只是感情好康愤,可當我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布儡循。 她就那樣靜靜地躺著,像睡著了一般征冷。 火紅的嫁衣襯著肌膚如雪择膝。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天检激,我揣著相機與錄音肴捉,去河邊找鬼。 笑死叔收,一個胖子當著我的面吹牛齿穗,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播饺律,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼窃页,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了复濒?” 一聲冷哼從身側(cè)響起脖卖,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎巧颈,沒想到半個月后畦木,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡砸泛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年十籍,在試婚紗的時候發(fā)現(xiàn)自己被綠了蛆封。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡勾栗,死狀恐怖惨篱,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情围俘,我是刑警寧澤妒蛇,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站楷拳,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏吏奸。R本人自食惡果不足惜欢揖,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望奋蔚。 院中可真熱鬧她混,春花似錦、人聲如沸泊碑。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽馒过。三九已至臭脓,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間腹忽,已是汗流浹背来累。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留窘奏,地道東北人嘹锁。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像着裹,于是被迫代替她去往敵國和親领猾。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,864評論 2 354

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