UIPageViewController 不显示其视图控制器视图
Posted
技术标签:
【中文标题】UIPageViewController 不显示其视图控制器视图【英文标题】:UIPageViewController does not show its view controllers views 【发布时间】:2015-03-23 05:59:07 【问题描述】:我在 UIViewController
中有一个 UIPageViewController
,我遇到了一个问题,即没有显示 pageviewcontroller 的内部视图 [尽管我确信它们是从服务器加载的]!
一些有用的说明:
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!
- (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
【问题讨论】:
【参考方案1】:对此不确定,但也许您应该使用自动调整大小的掩码来确保 OfferBannerPageViewController 的视图与父视图一起调整宽度和高度。可能会出现当你根据 superview 的 bounds 设置 pageController.view 的 frame 时,bounds 是 0 宽高,然后不调整大小。
那就试试吧:
[[self.pageController view] setFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)];
[[self.pageController view] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
【讨论】:
【参考方案2】:您是否实现了presentationCountForPageViewController
、presentationIndexForPageViewController
、viewControllerBeforeViewController
和 viewControllerAfterViewController
方法?
请给他们看。
【讨论】:
相信获取索引的时候应该使用“pageIndex”而不是“index” NSInteger index =((OfferBannerPageViewController *)viewController).pageIndex; 感谢您的努力,我不确定,因为我在 OfferBannerPageViewController 类中没有 pageIndex 属性 啊,对。看一个例子,希望你能发现你的代码有什么问题:dropbox.com/s/3ik34ydd675lu67/PageViewDemo.zip?dl=0以上是关于UIPageViewController 不显示其视图控制器视图的主要内容,如果未能解决你的问题,请参考以下文章
UIPageControl(页面控件)不显示在 UIPageViewController
如何重新加载UIPageViewController以在Swift中重新加载其视图
在 UINavigationController 中嵌套 UIPageViewController 只显示一页