I am stuck at presenting a full image on tapping t

2019-09-20 08:14发布

问题:

I have a UICollectionViewController in my application and I wish to make the image in a particular cell of full size when the user taps on it.

Sorry for the hassle, as I am a beginner for iOS. As per my knowledge will it be presented in a new View controller and if yes/no, then how?

Thanks in advance.

回答1:

If using storyboard, just add new ViewController to the scene and Ctrl-drag from the CollectionView cell to the ViewController to create segue. Choose modal if you want full screen.

In your CollectionViewController class, you need to pass the image reference to a property of the Detail ViewController. You need to use a public property, as the Outlet for the image is not set yet when the detailVC loads. After its instantiated, you just set the image to the Outlet to display it.

MyCVC.m (subclass of UICollectionViewController)

#import "MyCVC.h"
#import "DetailVC.h"

@interface MyCVC ()

@end

@implementation MyCVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"detailSegue"] && [segue.destinationViewController isKindOfClass:[DetailVC class]]) {

        DetailVC *detailVC = segue.destinationViewController;
        detailVC.image = sender.image; // or any other reference to the actual image to display
    }
}

@end

DetailVC.h

#import <UIKit/UIKit.h>

@interface DetailVC : UIViewController

@property (strong, nonatomic) UIImage *image;

@end

DetailVC.m

#import "DetailVC.h"

@interface DetailVC ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation DetailVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.imageView.image = self.image;
}

@end