1
私はobjective-cでのコーディングの初心者です。NSURLSessionが完了した後にpageViewControllerを作成します。
私は現在、NSURLSessionを使用してAPIからjson-dataを取得し、PageViewControllerに日付を送信する必要がある練習アプリケーションを作成しています。
現在、私は、(viewDidLoadで)pageViewControllerを作成する前に呼び出すメソッド 'getWeer'でjson-dataを取得しています。しかし、NSURLSessionはasyncを実行するので、私が思うには完了していないので、json-dataは、私のpageViewControllerでアクセスしようとすると、常にnullです。
NSURLSessionの完了後、どのようにしてpageViewControllerを作成できますか?
@interface SecondViewController()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) NSString *AppId;
@property (nonatomic, weak) NSDictionary *json;
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.AppId = @"feda1f13263bb730deeb89fb3936a76e";
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[[self locationManager] requestWhenInUseAuthorization];
[[self locationManager] startUpdatingLocation];
[self getWeer];
// Create page view controller
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
self.pageViewController.dataSource = self;
PageContentViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = @[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
// Change the size of page view controller
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);
[self addChildViewController:_pageViewController];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index{
// Create a new view controller and pass suitable data.
PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"];
pageContentViewController.pageIndex = index;
NSLog(@"%@",self.json);
pageContentViewController.json = self.json;
return pageContentViewController;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if (index == NSNotFound) {
return nil;
}
index++;
if (index == 3) {
return nil;
}
return [self viewControllerAtIndex:index];
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController{
return 3;
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController{
return 0;
}
-(void)getWeer{
NSString *dataUrl = [NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/forecast/daily?lat=%f&lon=%f&cnt=4&&APPID=%@&units=metric&lang=nl", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude, self.AppId];
NSLog(@"%f",self.locationManager.location.coordinate.latitude);
NSLog(@"%f",self.locationManager.location.coordinate.longitude);
NSURL *url = [NSURL URLWithString:dataUrl];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
self.json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
});
}];
[dataTask resume];
}
@end