页面视图控制器:UILabel和UIImageview在第一页上为空



我正在使用UIPageViewController来显示JSON文件中的项目。除了第一页在加载时不显示任何内容外,它工作正常,但是如果我回来,它可以工作。

代码如下:

#import "SJPagesViewController.h"
#import "SJChildViewController.h"
@interface SJPagesViewController ()
@end
@implementation SJPagesViewController
@synthesize urlToFollow, data,articlesArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
   ...
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    CGFloat window_height = ([self window_height]-30.0);
    CGFloat window_width  = [self window_width];
    CGRect pageFrame = CGRectMake(0.0f, 0.0f,window_width , window_height);
    self.pageController.dataSource = self;
    [[self.pageController view] setFrame:pageFrame];
    SJChildViewController *initialViewController = [self viewControllerAtIndex:0];
    NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];
    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    [self addChildViewController:self.pageController];
    [[self view] addSubview:[self.pageController view]];
    [self.pageController didMoveToParentViewController:self];

    //Download JSON
    NSError *error=nil;
    NSURL *url = [NSURL URLWithString:urlToFollow];
    data = [NSData dataWithContentsOfURL: url options:NSDataReadingMappedIfSafe error:&error];
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    NSArray *response = [dictionary objectForKey:@"items"];
    articlesArray = [[NSArray alloc] initWithArray:response];
    //NSLog(@"articlesArray = %@", articlesArray);

}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSUInteger index = [(SJChildViewController *)viewController index];
    if (index == 0) {
        return nil;
    }
    index--;
    return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSUInteger index = [(SJChildViewController *)viewController index];
    index++;
    if (index == articlesArray.count) {
        return nil;
    }
    return [self viewControllerAtIndex:index];
}
- (CGFloat) window_height   {
    return [UIScreen mainScreen].applicationFrame.size.height;
}
- (CGFloat) window_width   {
    return [UIScreen mainScreen].applicationFrame.size.width;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (SJChildViewController *)viewControllerAtIndex:(NSUInteger)index {
    SJChildViewController *childViewController = [[SJChildViewController alloc] initWithNibName:@"SJChildViewController" bundle:nil];
    childViewController.index = index;
    childViewController.arrayCount = self.articlesArray.count;
    childViewController.model = [[self.articlesArray objectAtIndex:index]objectForKey:@"modelo"];
    childViewController.price = [[self.articlesArray objectAtIndex:index]objectForKey:@"precio"];
    childViewController.make = [[self.articlesArray objectAtIndex:index]objectForKey:@"marca"];
    childViewController.imageUrl = [[self.articlesArray objectAtIndex:index]objectForKey:@"photoUrl"];
    return childViewController;
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
    // The number of items reflected in the page indicator.
    if (articlesArray.count >5) {
        return 5;
    }else return [articlesArray count];
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
    // The selected item reflected in the page indicator.
    return 0;
}
@end

和儿童视图控制器显示项目:

@implementation SJChildViewController
@synthesize activityIndicator,imageUrl,price,model,make;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    ....
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    //Activity indicator
    activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityIndicator.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0);
    [self.view addSubview: activityIndicator];
    [activityIndicator startAnimating];
}
-(void)viewDidAppear:(BOOL)animated{
    self.scrrenNumber.text = [NSString stringWithFormat:@"Artículo %ld de %ld", ((long)self.index+1), (long)self.arrayCount];
    self.lblMake.lineBreakMode = NSLineBreakByWordWrapping;
    self.lblMake.text = [NSString stringWithFormat:@"%@",make];
    //Donload image
    //Download image from url
    NSURL *url_1= [NSURL URLWithString:imageUrl];
    NSURLRequest *request_1 = [NSURLRequest   requestWithURL:url_1];
    [NSURLConnection sendAsynchronousRequest:request_1
                                       queue:[NSOperationQueue currentQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
                               UIImage  *img = [UIImage imageWithData:data];
                               [self.articleImage performSelectorOnMainThread:@selector(setImage:) withObject:img waitUntilDone:YES];
                           }];
    //Activity indicator
    self.lblPrice.text = [NSString stringWithFormat:@"%@",price];
    self.lblModel.lineBreakMode = NSLineBreakByWordWrapping;
    self.lblModel.text = [NSString stringWithFormat:@"%@",model];
    [activityIndicator stopAnimating];
}

我怎样才能让它从一开始就起作用?

路易斯,我现在无法访问 xcode,但似乎您的问题是您在articlesArray为空时正在实例化childViewController

viewDidLoad,您将下载 json 文件并在调用帮助程序方法 viewControllerAtIndex 后填充articlesArray

尝试下载您的 json 文件并首先填充数组,就像您在这里一样。

//Download JSON
NSError *error=nil;
NSURL *url = [NSURL URLWithString:urlToFollow];
data = [NSData dataWithContentsOfURL: url options:NSDataReadingMappedIfSafe error:&error];
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSArray *response = [dictionary objectForKey:@"items"];
articlesArray = [[NSArray alloc] initWithArray:response];

然后创建您的页面视图控制器并像您在此处所做的那样填充它。

self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
CGFloat window_height = ([self window_height]-30.0);
CGFloat window_width  = [self window_width];
CGRect pageFrame = CGRectMake(0.0f, 0.0f,window_width , window_height);
self.pageController.dataSource = self;
[[self.pageController view] setFrame:pageFrame];
SJChildViewController *initialViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];

最新更新