简体   繁体   中英

Segue Identifier on iPad Storyboard

I have an app set up with collection views one has a series of cells on then segues to another with a full screen cell on to recreate a photo gallery. I have this working fine on the iPhone but when I try on iPad clicking on the first cell should recognise item at index path and pass this to the full screen view, which is not happening.

I believe the reason to be I am looking for a segue identifier and as this is a universal app am using the same code for both uses. On the iPhone storyboard I am able to set the segue identifier but this does not appear in the iPad storyboard. The question is…. Is this normal and is there a way around it? I can provide code on request but didn't feel it relevant to the question at this time.

Code is:

First Collection View

@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

Detail View

@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

Just seen that you said "does not appear in the iPad storyboard".

It won't just appear without setting it yourself.

You'll need to go into the iPad storyboard file and set the segue identifier again in there.

If you are using the same storyboard as of iPhone with iPad, then you could simply copy the contents of iPhone storyboard and paste it iPad storyboard. Things would be misplaced but if using auto layout it would be fixed at run time. Or you could follow this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM