【发布时间】:2014-01-23 19:52:51
【问题描述】:
我有一个应用程序设置了收藏视图,其中一个具有一系列单元格,然后与另一个具有全屏单元格的单元格连接以重新创建照片库。我在 iPhone 上可以正常工作,但是当我尝试在 iPad 上单击第一个单元格时,应该可以识别索引路径中的项目并将其传递给全屏视图,但这种情况没有发生。
我相信我正在寻找一个 segue 标识符的原因,因为这是一个通用应用程序,所以我对两种用途都使用相同的代码。在 iPhone 故事板上,我可以设置 segue 标识符,但这不会出现在 iPad 故事板上。问题是…。这是正常的吗?有办法解决吗?我可以根据要求提供代码,但目前认为它与问题无关。
代码是:
第一个集合视图
@implementation Study3CollectionViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createData];
}
- (void)createData {
self.dresserImages = [NSMutableArray array];
[self.dresserImages addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:@"Newton Wardrobes", @"name",
@"IMG_4723.JPG", @"image", nil]];
[self.dresserImages addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:@"Newton Wardrobes", @"name",
@"IMG_4726.JPG", @"image", nil]];
[self.dresserImages addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:@"Newton Wardrobes", @"name",
@"IMG_4729.JPG", @"image", nil]];
[self.dresserImages addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:@"Newton Wardrobes", @"name",
@"IMG_4730.JPG", @"image", nil]];
[self.dresserImages addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:@"Newton Wardrobes", @"name",
@"IMG_4731.JPG", @"image", nil]];
[self.collectionView reloadData];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dresserImages.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *dresserImageView = (UIImageView *)[cell viewWithTag:100];
dresserImageView.image = [UIImage imageNamed:[[self.dresserImages objectAtIndex:indexPath.row] objectForKey:@"image"]];
return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (_startingIndexPath) {
NSInteger currentIndex = floor((scrollView.contentOffset.x - scrollView.bounds.size.width / 1) / scrollView.bounds.size.width) + 1;
if (currentIndex < [self.dresserImages count]) {
self.title = self.dresserImages[currentIndex][@"name"];
}
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"collectionView"])
{
Study3DetailCollectionViewController *destViewController = (Study3DetailCollectionViewController *)segue.destinationViewController;
NSIndexPath *indexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0];
destViewController.startingIndexPath = indexPath;
[destViewController.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
[self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.collectionView scrollToItemAtIndexPath:self.startingIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
详细视图
@implementation Study3DetailCollectionViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createData];
}
- (void)createData {
self.dresserImages = [NSMutableArray array];
[self.dresserImages addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:@"Newton Wardrobes", @"name",
@"IMG_4723.JPG", @"image", nil]];
[self.dresserImages addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:@"Newton Wardrobes", @"name",
@"IMG_4726.JPG", @"image", nil]];
[self.dresserImages addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:@"Newton Wardrobes", @"name",
@"IMG_4729.JPG", @"image", nil]];
[self.dresserImages addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:@"Newton Wardrobes", @"name",
@"IMG_4730.JPG", @"image", nil]];
[self.dresserImages addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:@"Newton Wardrobes", @"name",
@"IMG_4731.JPG", @"image", nil]];
[self.collectionView reloadData];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dresserImages.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *dresserImageView = (UIImageView *)[cell viewWithTag:100];
dresserImageView.image = [UIImage imageNamed:[[self.dresserImages objectAtIndex:indexPath.row] objectForKey:@"image"]];
return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (_startingIndexPath) {
NSInteger currentIndex = floor((scrollView.contentOffset.x - scrollView.bounds.size.width / 1) / scrollView.bounds.size.width) + 1;
if (currentIndex < [self.dresserImages count]) {
self.title = self.dresserImages[currentIndex][@"name"];
}
}
}
- (BOOL) shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
layout.itemSize = self.view.bounds.size;
[self.collectionView scrollToItemAtIndexPath:self.startingIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
【问题讨论】:
-
您是否在两个情节提要上设置了标识符?
-
是的,你应该提供代码,尤其是
prepareForSegues:sender:part -
如果您的应用是通用应用,则必须单独为 iPad 故事板设置 segue 标识符
-
我的意思是它没有出现是我没有选择输入它的选项,您通常输入的选项卡只是说不适用,因为它在 iPhone 选项卡上,并且我可以输入。 iPhone 工作得很好。
-
你能显示标签的截图吗? :)
标签: ios ipad storyboard uistoryboardsegue