简体   繁体   English

如何从以编程方式创建的UIImageView中选择

[英]How do I segue from programmatically created UIImageView

I have programmatically created UIImage instances which increases as the number of imageArray count increases and UIImage are set to them programmatically. 我已经以编程方式创建了UIImage实例, UIImage实例随着imageArray计数的增加而增加,并且以编程方式将UIImage设置为它们。 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. 由于这些UIImageView中不可见的故事板,我怎样才能从Segue公司UIViewController另一个UIViewController包含的一个实例UIImage显示详细图像。 How to segue to another UIViewController when any of my UIImageView is touched. 当我的任何UIImageView被触摸时,如何选择另一个UIViewController

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: . 无论您如何设置视图控制器,该控制器始终可以使用-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: 因此,如果您已设置情节myDetailSegue以使标识为myDetailSegue的segue从图像阵列视图控制器通向图像细节视图控制器,则图像阵列控制器可以执行以下操作:

[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. 现在,您只需要在每个图像视图上附加一个手势识别器,即可将其目标设置为图像阵列视图控制器,并将操作设置为新的goToDetailController操作。 You'll also need a -prepareForSegue:sender: method just like you always do when using segues. 就像使用segues一样,您还需要-prepareForSegue:sender:方法。 That method can set the detail view controller's image to self.detailImage , since you wisely saved that in the action. 该方法可以将细节视图控制器的图像设置为self.detailImage ,因为您明智地将其保存在了动作中。

You can use UICollectionView for show, the images. 您可以使用UICollectionView来显示图像。 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. 每个单元格都有一个标识符(例如indexPathUITableview ),因此,对于每个要触摸的单元格,将使用此值并将其保留为图像的ID。 Search the image connected for this ID and load in DetailViewController . 在连接的图像中搜索此ID,并将其加载到DetailViewController

UICollectionView has a method that detects which cell has been touched. UICollectionView具有一种检测已触摸哪个单元格的方法。

You can share the imgNameArray variable using a Singleton. 您可以使用Singleton共享imgNameArray变量。 And using the same "ID" and send to DetailViewController , you can load the same image referenced. 并使用相同的“ ID”并将其发送到DetailViewController ,您可以加载引用的相同图像。

This is one possible solution. 这是一种可能的解决方案。

Add UITapGestureRecognizer to each of your UIImageview and define unique tag for each Imageview (as per your forloop). 将UITapGestureRecognizer添加到每个UIImageview中,并为每个Imageview定义唯一的标签(根据您的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. 并执行Segue。

You can use UIButton like this below. 您可以在下面使用UIButton。

Set its BG image and put tag each button. 设置其BG图像,并标记每个按钮。

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];
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从以编程方式创建的UITableView中进行筛选? - How do I segue from a programmatically created UITableView? 如何在 Swift 中以编程方式阻止 segue? - How do I stop a segue from going through programmatically in Swift? 如何以编程方式将 UILabel 放置在 UIImageview 上? - How do I programmatically place UILabel on UIImageview? 从以编程方式创建的表视图创建 segue - Creating segue from programmatically created table view 如何以编程方式在UIScrollView中垂直居中放置UIImageView? - How do I vertically center a UIImageView in a UIScrollView programmatically? 以编程方式创建的UIImageView - UIImageView Created Programmatically 如何以编程方式将UIImage添加到UIImageView然后将该UIImageView添加到另一个UIView? - How do I programmatically add a UIImage to a UIImageView then add that UIImageView to another UIView? 如何从UINavigationController选择到UIViewController - How do I segue from a UINavigationController to a UIViewController 如何访问以编程方式创建的按钮? - How do I access programmatically created buttons? ios从程序创建的按钮segue打开模态? - ios open modal from segue from a programmatically created button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM