UIPageViewController不显示其视图控制器视图

时间:2015-03-23 05:59:08

标签: ios objective-c uiviewcontroller uipageviewcontroller

我在UIPageViewController中有一个UIViewController,我遇到的问题是未显示pageviewcontroller的内部视图[虽然我确定它们是从服务器加载的]!< / p>

一些有用的说明:

UIPageViewController(名为: pageController

UIViewController(名为: OfferDetailsViewController

pageController中的每个viewController都是OfferBannerPageViewController

类型

以下是与此问题相关的代码,如果您认为有助于查找问题来源,请询问任何其他代码块

部分内容: OfferDetailsViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    if(_offerId)
    {
        _offer = [[Offer alloc]init];
        [delegate setUpAConnectionToGetADetailedOffer:_offerId];
        //first: load the offer
        [self loadOffer];
        [self startTimer];
    }
}

-(void) loadOffer{
        NSMutableString * pathPattern = [NSMutableString stringWithString: @"api/Offers/"];

        [pathPattern appendString:_offerId];

        [[RKObjectManager sharedManager]getObjectsAtPath:pathPattern parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
            if(mappingResult){
                _offer = (Offer *)[mappingResult.array firstObject];
                //second load the offer banners images
                [self setOfferView];

                dispatch_async(dispatch_get_main_queue(), ^{
                    [self loadOfferBanners];
                });

            }else{
            }
        }
        failure:^(RKObjectRequestOperation *operation, NSError *error) {
        }];
}

-(void)loadOfferBanners {

    _offer.OfferPannerImagesObjects = [[NSMutableArray alloc]init];

    for(int index = 0 ; index<_offer.OfferPannerImages.count ;index++){
        NSURL *imageURL =[NSURL URLWithString:_offer.OfferPannerImages[index]];
        NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
        UIImage *image = [UIImage imageWithData:imageData];
        //[UIImage imageWithData:imageData];
        if(image){
            [_offer.OfferPannerImagesObjects addObject: image];
        }
    }
    if(_offer.OfferPannerImagesObjects.count>0){
        [self setUpOfferBanners];
    }
}

-(void)setUpOfferBanners {

self.pageController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.dataSource = self;
    CGRect rect = [self.pagesView bounds];
    [[self.pageController view] setFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)];

    OfferBannerPageViewController * initialBanner = [self viewControllerAtIndex: 0];
    [self.pageController setViewControllers:[NSArray arrayWithObjects:initialBanner, nil] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil ];

    [self addChildViewController:self.pageController];
    [[self pagesView] addSubview:[self.pageController view]];
    [self.pageController didMoveToParentViewController:self];

    self.pagesView.clipsToBounds=YES;
    self.pagesView.layer.cornerRadius= 6;
}

这里是ViewControllerAtIndex方法我调试它并发现索引总是如此,而当我在断点处停留后将其值视为0时,我将其值视为0!

- (OfferBannerPageViewController *)viewControllerAtIndex:(NSUInteger)index {

    OfferBannerPageViewController *offerBannerPageViewController = [[OfferBannerPageViewController alloc] initWithNibName:@"OfferBannerPageViewController" bundle:nil];

    offerBannerPageViewController.bannerView = _offer.OfferPannerImagesObjects[index];

    offerBannerPageViewController.index = [NSNumber numberWithLong:(long)index];
    NSLog(@"____________________ [INDEX = %ld] ___________________",(long)index);

    [offerBannerPageViewController.bannerView setImage:_offer.OfferPannerImagesObjects[index]];

    return offerBannerPageViewController;
}

任何帮助都将非常感谢,谢谢。

更新

- (NSInteger)presentationCountForPageViewController: (UIPageViewController*)pageViewController {
    // The number of items reflected in the page indicator.
    return [_offer.OfferPannerImages count];
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
    // The selected item reflected in the page indicator.
    return 0;
}

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{

    NSInteger index = (long)((OfferBannerPageViewController *)viewController).index;
    if(index >= _offer.OfferPannerImagesObjects.count){
        return nil;
    }
    index ++;
    return [self viewControllerAtIndex:index];
}

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSInteger index = (long)((OfferBannerPageViewController *)viewController).index;
    if(index == 0)
        return nil;
    index--;
    return [self viewControllerAtIndex:index];
}

更新2:OfferBannerPageViewController.h

@interface OfferBannerPageViewController : UIViewController
@property (strong,nonatomic) NSNumber * index;
@property (strong,nonatomic)IBOutlet UIImageView * bannerView;
@end

2 个答案:

答案 0 :(得分:0)

您是否实施了presentationCountForPageViewControllerpresentationIndexForPageViewControllerviewControllerBeforeViewController viewControllerAfterViewController方法? 请告诉他们。

答案 1 :(得分:0)

不确定这一点,但也许你应该使用autoresizing mask来确保OfferBannerPageViewController的视图与superview一起调整宽度和高度。可能会发生这种情况,当你根据superview的边界设置pageController.view的框架时,边界是0宽度和高度,然后不调整大小。

所以试试:

[[self.pageController view] setFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)];
[[self.pageController view] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];