Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

ios - I am stuck at presenting a full image on tapping the cell in UICollectionViewController

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...