我一直在寻找答案但没有成功。我希望能够在UICollectionView中选择一个项目并转到其详细视图,一个UIPageViewController水平滚动三个不同的视图控制器。
问题在于,当使用UIPageViewController时,我无法弄清楚如何将数据从主视图传递到详细视图。我可以将它传递给普通的detailViewController,但是使用UIPageViewController,我的listItems在到达pageViewController时为null。
如何将图像和标签传递给我的page1ViewController,我想我的detailPagingViewController中缺少一步?我还在学习,非常感谢所有的帮助!
这是故事板: http://pbrd.co/1y2rdB2
在collectionViewController中:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showDetailPageVC"]) {
NSIndexPath *indexPath = [self.collectionView indexPathForCell:sender];
DetailPagingViewController *destViewController = segue.destinationViewController;
PFObject *object = [imagesFilesArray objectAtIndex:indexPath.row];
ListItems *listItem = [[ListItems alloc] init];
listItem.name = [object objectForKey:@"name"];
listItem.imageFile = [object objectForKey:@"imageFile"];
destViewController.listItem = listItem;
}
}
详细信息PageViewController.h
@interface DetailPagingViewController : UIPageViewController
<UIPageViewControllerDelegate, UIPageViewControllerDataSource>
@property (nonatomic, strong) ListItems *listItem;
@end
detailPagingViewController.m
@interface DetailPagingViewController ()
@property (strong, nonatomic) UIPageViewController *pageViewController;
@property (strong, nonatomic) NSArray *contentPageRestorationIDs;
@end
@implementation DetailPagingViewController
{
NSArray *myViewControllers;
}
@synthesize listItem;
-(void)viewDidLoad
{
[super viewDidLoad];
self.delegate = self;
self.dataSource = self;
UIViewController *p1 = [self.storyboard
instantiateViewControllerWithIdentifier:@"Page1VC"];
UIViewController *p2 = [self.storyboard
instantiateViewControllerWithIdentifier:@"Page2VC"];
UIViewController *p3 = [self.storyboard
instantiateViewControllerWithIdentifier:@"Page3VC"];
myViewControllers = @[p1,p2,p3];
[self setViewControllers:@[p1]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO completion:nil];
}
-(UIViewController *)viewControllerAtIndex:(NSUInteger)index
{
return myViewControllers[index];
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger currentIndex = [myViewControllers indexOfObject:viewController];
--currentIndex;
currentIndex = currentIndex % (myViewControllers.count);
return [myViewControllers objectAtIndex:currentIndex];
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger currentIndex = [myViewControllers indexOfObject:viewController];
++currentIndex;
currentIndex = currentIndex % (myViewControllers.count);
return [myViewControllers objectAtIndex:currentIndex];
}
-(NSInteger)presentationCountForPageViewController:
(UIPageViewController *)pageViewController
{
return myViewControllers.count;
}
-(NSInteger)presentationIndexForPageViewController:
(UIPageViewController *)pageViewController
{
return 0;
}
@end
在pageViewController.h中
@interface Page1ViewController : UIViewController
@property (weak, nonatomic) IBOutlet PFImageView *mainImage;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (nonatomic, strong) ListItems *listItem;
@end
在pageViewController.m中
@implementation Page1ViewController
@synthesize mainImage, listItem, nameLabel;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
mainImage.file = listItem.imageFile;
nameLabel.text = listItem.name;
}
@end