将数组索引传输到下一个控制器

时间:2013-05-20 05:54:06

标签: iphone ios objective-c

我有一个array.getting图像来自该数组我希望将数组索引传递给下一个方法我该怎么做呢。

我的代码: -

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

随机取出所有图像并垂直存储imageview滚动

    randIdx=arc4random()%[FrontsCards count];

    NSString *imageName=[FrontsCards objectAtIndex:randIdx];

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

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

我可以提前感谢你如何做到这一点。

5 个答案:

答案 0 :(得分:3)

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

创建appDelegate 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 ......

答案 1 :(得分:1)

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

@property(nonatomic)NSInteger tagValue;

首先为imageView设置标记

[ImgView setTag:randIdx]; 

将Gesture rec添加到yourImageView

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

然后在你的double tapGesture上,获取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];

}

答案 2 :(得分: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);
    }

答案 3 :(得分: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;

}

答案 4 :(得分:0)

除了@ Rajneesh071答案集

randIdx=arc4random()%[FrontsCards count];
NSString *imageName=[FrontsCards objectAtIndex:randIdx];
[ImgView setImage:[UIImage imageNamed:imageName]];
//ADD THE TAG
[ImgView setTag:randIdx];