I have programmatically created UIImage
instances which increases as the number of imageArray count increases and UIImage
are set to them programmatically. Since those UIImageView
are not visible in Storyboard, how can I segue from that UIViewController
to another UIViewController
containing an instance of UIImage
to show image in detail. How to segue to another UIViewController
when any of my UIImageView
is touched.
Here is the code:
#import "FirstPageViewController.h"
@interface FirstPageViewController ()
@property (nonatomic, strong) UIImageView *imageView;
@property UIImage *image;
@property NSArray *imgArray;;
@property NSMutableArray *imgNameArray;
@property UIView *containerView;
@property UIView *containerView2;
@end
@implementation FirstPageViewController
for (NSString *img in imgArray) {
[imgNameArray addObject:img];
}
int Y = 0;
int X = 0;
for (int i = 0; i<imgNameArray.count; i++) {
NSString *imgName = imgNameArray[i];
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imgName]];
imageView.frame = CGRectMake(X, Y, 145, 109);
imageView.layer.cornerRadius = 2;
imageView.clipsToBounds = YES;
[imageView.layer setBorderWidth:2];
[imageView.layer setBorderColor:[[UIColor whiteColor]CGColor ]];
if ((i%2)==0) {
X = 154;
}else {
X = 0;
}
if ((i%2) ==0) {
Y = i * 59;
}
[containerView addSubview:imageView];
}
[self.scrollView addSubview: containerView];
self.scrollView.contentSize = CGSizeMake(320, imgNameArray.count * 67);
No matter how you've set up your view controller, the controller can always segue to another view controller using -performSegueWithIdentifier:sender:
. So if you've set up your storyboard so that there's a segue with the identifier myDetailSegue
leading from the image array view controller to the image detail view controller, the image array controller can do this:
[self performSegueWithIdentifier:@"myDetailSegue" sender:self];
So, one way to handle your task is to create an action in your image array controller:
-(IBAction)goToDetailController:(UIGestureRecognizer*)sender {
// get the image view from the gesture recognizer
UIImageView *tappedView = (UIImageView*)sender.view;
// save the image from the image view
self.detailImage = tappedView.image;
// start the detail segue
[self performSegueWithIdentifier:@"myDetailSegue" sender:self];
}
Now you just need to attach a gesture recognizer to each image view, setting it's target to the image array view controller and action to your new goToDetailController
action. You'll also need a -prepareForSegue:sender:
method just like you always do when using segues. That method can set the detail view controller's image to self.detailImage
, since you wisely saved that in the action.
You can use UICollectionView
for show, the images. Each cell have a identifier (like indexPath
from UITableview
) so, for each cell that will touched you will take this value and reserve this like a ID for your images. Search the image connected for this ID and load in DetailViewController
.
UICollectionView
has a method that detects which cell has been touched.
You can share the imgNameArray
variable using a Singleton. And using the same "ID" and send to DetailViewController
, you can load the same image referenced.
This is one possible solution.
Add UITapGestureRecognizer to each of your UIImageview and define unique tag for each Imageview (as per your forloop).
In you gesture recognizer method, you can get tag by
[(UIGestureRecognizer *)sender view].tag
Use this tag to get perticular image from your array. And performSegue.
You can use UIButton like this below.
Set its BG image and put tag each button.
Then in its selector, check tag to determine which image to be passed.
UIButton *btn = [[UIButton alloc] initWithFrame:BUTTON_RECT];
[btn setBackgroundImage:YOUR_IMAGES forState:UIControlStateNormal];
[btn addTarget:self action:@selector(handlePushEvent:) forControlEvents:UIControlEventTouchUpInside];
[btn setTag:i]; // you can set your defined tag here plus int i in your loop
.
.
.
[self.scrollView addSubview:btn];
- (void) handlePushEvent:(UIButton *)sender
{
// PERFORM SEGUE HERE or YOU CAN ALSO PUSH THE VC
int btnTag = sender.tag;
for(int ctr=0; ctr<imgNameArray.count; ctr++)
{
if(ctr == btnTag) {
NextViewController *nextVC = [[NextViewController alloc] initWithNibName:nil bundle:nil];
nextVC.passedImage = imgNameArray[ctr]; // create a property in your next VC, then assign like this here
[self.navigationController pushViewController:nextVC animated:YES];
}
}
}
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.