1

我有一个数组。从该数组中获取图像我想将数组索引传输到下一个方法我该怎么做。

我的代码:-

FrontsCards =[[NSMutableArray alloc]initWithCapacity:13];
    [FrontsCards insertObject:@"cloub1.png" atIndex:0];
    [FrontsCards insertObject:@"cloub2.png" atIndex:1];
    [FrontsCards insertObject:@"cloub3.png" atIndex:2];
    [FrontsCards insertObject:@"cloub4.png" atIndex:3];
    [FrontsCards insertObject:@"cloub5.png" atIndex:4];
    [FrontsCards insertObject:@"cloub6.png" atIndex:5];
    [FrontsCards insertObject:@"cloub7.png" atIndex:6];
    [FrontsCards insertObject:@"cloub8.png" atIndex:7];
    [FrontsCards insertObject:@"cloub9.png" atIndex:8];
    [FrontsCards insertObject:@"cloub10.png" atIndex:9];
    [FrontsCards insertObject:@"cloub11.png" atIndex:10];
    [FrontsCards insertObject:@"cloub12.png" atIndex:11];
    [FrontsCards insertObject:@"cloub13.png" atIndex:12];

随机获取所有图像并垂直存储图像视图滚动

    randIdx=arc4random()%[FrontsCards count];

    NSString *imageName=[FrontsCards objectAtIndex:randIdx];

   [ImgView setImage:[UIImage imageNamed:imageName]];

如果用户双击 cloub12.png 图像,我想生成 11 索引值并将其传输到下一个视图控制器。如果用户点击 cloub4 索引生成 3

我怎么能这样做提前谢谢。

4

5 回答 5

3

您可以通过创建将任何类型的数据传递到应用程序中的任何@property位置appDelegate。例如,如果您想将任何整数数据传递给某个视图控制器。您必须@propertyappDelegate---

appDelegate.h----

@property(nonatomic,assign)int someObj;

appDelegate.m-----

@synthesize someObj;

然后你可以通过导入 appDelegate 类来访问你想要的应用程序中的这个对象,你必须像这样创建一个单例类(appDelegate)的对象

AppDelegateClass  *appDelegate= [[UIApplication sharedApplication]delegate];

appDelegate.someObj=_theValueWhichYouToPass(/*here the value is integer*/).


//this assignment gives u this value anywhere you want through the object of appDelegate...

NSLog(@"%d",appDelegateObj.someObj);

您可以随意使用此值..

              Hope this will help u very much ......
于 2013-05-20T11:51:25.900 回答
1

设置属性,并在 NextViewController 中合成它

@property(nonatomic)NSInteger tagValue;

首先将标签设置为您的 imageView

[ImgView setTag:randIdx]; 

将手势记录添加到 yourImageView

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap:)];
     tap.cancelsTouchesInView = YES;
     tap.numberOfTapsRequired = 2;
     tap.delegate = self;
     [ImgView addGestureRecognizer:tap];

然后在你的双击手势上,获取 yourImage 的标签值然后

// 处理方法

- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer {
    UIImageView *imageView = (UIImageView *)[gestureRecognizer view];
    NSLog(@"Pass this tag=%d",image.tag);

    NextViewController *NVC=[[NextViewController alloc]initWithNibName:@"NextViewController" bundle:nil];
    NVC.tagValue=yourImageView.tag;
    [self.navigationController pushViewController:NVC animated:YES];

}
于 2013-05-20T06:01:27.847 回答
1
     randIdx=arc4random()%[FrontsCards count];
     NSString *imageName=[FrontsCards objectAtIndex:randIdx];
     [ImgView setImage:[UIImage imageNamed:imageName]];
     ImgView.tag=randIdx;

     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap:)];
     tap.cancelsTouchesInView = YES;
     tap.numberOfTapsRequired = 2;
     tap.delegate = self;
     [ImgView addGestureRecognizer:tap];
     [tap release];

     ImgView.userInteractionEnabled = YES;

   // handle method
   - (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer { 
      UIImageView *imageView = (UIImageView *)[gestureRecognizer view];
       UIImage *image = [imageView image];
       NSLog(@"Pass this tag=%d",image.tag);
    }
于 2013-05-20T06:13:50.157 回答
0

您可以通过覆盖给定的 init 方法将索引传递给 nextviewController,

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andIndex:(NSUInteger)index {

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if(self) {

    self.index = index;
}

return self;

}

或者

- (id)initWithIndex:(NSUInteger)index {

self = [super init];

if(self) {

    self.index = index;
}

return self;

}

于 2013-05-20T06:02:23.123 回答
0

除了@Rajneesh071 答案集

randIdx=arc4random()%[FrontsCards count];
NSString *imageName=[FrontsCards objectAtIndex:randIdx];
[ImgView setImage:[UIImage imageNamed:imageName]];
//ADD THE TAG
[ImgView setTag:randIdx];
于 2013-05-20T06:09:59.043 回答