2018-07-01

使用MVC模式

创建LodData继承NSObject

LodData.h中创建单例模式、解析天气

// 创建单利方法

+(instancetype)showCityMessage;

// 解析城市天气预报方法

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

LodData.m

@interface LoadData()

{

    // 创建可变数组

    NSMutableDictionary*_cityWeatherDic;

}

@end

// 创建单利变量

static LoadData *ld;

@implementation LoadData

// 实现创建单利方法

+(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;

}

// 实现解析城市天气预报方法

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

    NSLog(@"%@",url);

    // 创建URl解析

    NSURLSession *session = [NSURLSession sharedSession];

    _cityWeatherDic = [NSMutableDictionary dictionary];

    // 创建task

    NSURLSessionTask*task = [sessiondataTaskWithURL:[NSURLURLWithString:url]completionHandler:^(NSData*_Nullabledata,NSURLResponse*_Nullableresponse,NSError*_Nullableerror) {


        self->_cityWeatherDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

//        NSLog(@"数据==%@",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"

// 创建接口

#define WEATHER_URL @"https://www.sojson.com/open/api/weather/json.shtml?city=%@"

@interface ViewController (){

    // 创建对象

    LoadData*ld;

    WeatherViewController *weatherVc;

}

@property (strong, nonatomic) IBOutlet UILabel *cityLb;

@property (strong, nonatomic) IBOutlet UITextField *cityTF;

// 创建可变数组接收数据

//@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 - 注册通知接收数据方法

- (void)getCityWeather:(NSNotification*)notifi{

    //    // 初始化对象

    weatherVc = [[WeatherViewController alloc]init];

    weatherVc.messageDic = notifi.object;

//    NSLog(@"===%@",weatherVc.messageDic);

    // 跳转

    [self presentViewController:weatherVc animated:YES completion:nil];

}

// 点击按钮方法

- (IBAction)cityBtn:(id)sender {

    NSString*data =self.cityTF.text;

    //转换成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

你可能感兴趣的:(2018-07-01)