[英]How do I segue from programmatically created UIImageView
我已經以編程方式創建了UIImage
實例, UIImage
實例隨着imageArray計數的增加而增加,並且以編程方式將UIImage
設置為它們。 由於這些UIImageView
中不可見的故事板,我怎樣才能從Segue公司UIViewController
另一個UIViewController
包含的一個實例UIImage
顯示詳細圖像。 當我的任何UIImageView
被觸摸時,如何選擇另一個UIViewController
。
這是代碼:
#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);
無論您如何設置視圖控制器,該控制器始終可以使用-performSegueWithIdentifier:sender:
來選擇另一個視圖控制器。 因此,如果您已設置情節myDetailSegue
以使標識為myDetailSegue
的segue從圖像陣列視圖控制器通向圖像細節視圖控制器,則圖像陣列控制器可以執行以下操作:
[self performSegueWithIdentifier:@"myDetailSegue" sender:self];
因此,處理任務的一種方法是在圖像陣列控制器中創建一個動作:
-(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];
}
現在,您只需要在每個圖像視圖上附加一個手勢識別器,即可將其目標設置為圖像陣列視圖控制器,並將操作設置為新的goToDetailController
操作。 就像使用segues一樣,您還需要-prepareForSegue:sender:
方法。 該方法可以將細節視圖控制器的圖像設置為self.detailImage
,因為您明智地將其保存在了動作中。
您可以使用UICollectionView
來顯示圖像。 每個單元格都有一個標識符(例如indexPath
的UITableview
),因此,對於每個要觸摸的單元格,將使用此值並將其保留為圖像的ID。 在連接的圖像中搜索此ID,並將其加載到DetailViewController
。
UICollectionView
具有一種檢測已觸摸哪個單元格的方法。
您可以使用Singleton共享imgNameArray
變量。 並使用相同的“ ID”並將其發送到DetailViewController
,您可以加載引用的相同圖像。
這是一種可能的解決方案。
將UITapGestureRecognizer添加到每個UIImageview中,並為每個Imageview定義唯一的標簽(根據您的forloop)。
在手勢識別器方法中,您可以通過
[(UIGestureRecognizer *)sender view].tag
使用此標簽可以從陣列中獲取垂直圖像。 並執行Segue。
您可以在下面使用UIButton。
設置其BG圖像,並標記每個按鈕。
然后在其選擇器中,檢查標記以確定要傳遞的圖像。
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.