目标C IOS prepareForSegue我如何获得一个intvariables我分配给一个button到下一个视图控制器

所以我有一个集合视图,它使用核心数据dynamic地生成一组button。 我已经完成了将button和值分配给button的部分。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"Cell"; [self.collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:identifier]; CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; [cell.myButton addTarget:self action:@selector(collectionViewButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; UIImage *btnImage; btnImage = [UIImage imageNamed:[sportButtons objectAtIndex:indexPath.row]]; NSNumber *sportsValue; sportsValue =[NSNumber numberWithInt:(int)[sportsNumber objectAtIndex:indexPath.row]]; [cell.myButton setBackgroundImage:btnImage forState:UIControlStateNormal]; return cell; } 

所以这工作正常

现在这是代码执行Segue

 - (void)collectionViewButtonPressed:(UIButton *)sender { [self performSegueWithIdentifier:@"venues" sender:self]; } 

现在这个代码在这里如何获得单元格/button中的sportsValue传递给下一个视图控制器,所以我可以使用它作为一个variables

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"venues"]) { //so how do I get that sportsValue in the cell/button to pass it onto to the next view controller so I can use it as a variable } } 

这里是一个例子,你如何可以在collectionViewButtonPressed获得这个sportsValue并传给nextVC

FirstViewController.m class

 #import "YourNextVIewController.h" @interface FirstViewController (){ NSNumber *passSportsValue; // this will hold your sports value when button pressed. } @end - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"Cell"; [self.collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:identifier]; CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; [cell.myButton addTarget:self action:@selector(collectionViewButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; UIImage *btnImage; btnImage = [UIImage imageNamed:[sportButtons objectAtIndex:indexPath.row]]; NSInteger sportsValue; sportsValue =[[sportsNumber objectAtIndex:indexPath.row]integerValue]; [cell.myButton setBackgroundImage:btnImage forState:UIControlStateNormal]; cell.myButton.tag= sportsValue; // or cell.myButton.tag=indexPath.row; No need to get sportsValue in this method // you can also use cell.myButton.superview.tag property to pass your sportsValue, if you don't want to use myButton.tag property return cell; } - (void)collectionViewButtonPressed:(UIButton *)sender { passSportsValue = [NSNumber numberWithInteger:sender.tag]; // ie your sportsValue /* if you are using indexPath.row ie cell.myButton.tag = indexPath.row; passSportsValue = [NSNumber numberWithInteger:[[sportsNumber objectAtIndex:sender.tag]integerValue]]; */ [self performSegueWithIdentifier:@"venue" sender:self]; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"venue"]) { YourNextVIewController *nextVC = [segue destinationViewController]; nextVC.passNumber = passSportsValue; } } 

YourNextVIewController.h class添加这一行

 @property (strong, nonatomic) NSNumber *passNumber; 

现在,您可以在YourNextVIewController.m检查YourNextVIewController.m

 - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"%d",[self.passNumber integerValue]); } 

希望这会解决你的问题..

首先为每个button设置一个tag属性等于所需的index ,然后在selector方法中,您可以获得该index

 - (void)collectionViewButtonPressed:(UIButton *)sender { NSInteger index = sender.tag; [self performSegueWithIdentifier:@"venues" sender:index]; } 

请这只是逻辑:

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"venues"]) { UIViewController *zoomVC = segue.destinationViewController; UICollectionViewCell *cell = (UICollectionViewCell *) [self superviewWithClassName:@"UICollectionViewCell" fromView:button]; NSIndexPath *indexPath; if (cell) { indexPath = [self.collectionView indexPathForCell:cell]; // do whatever action you need with the indexPath... } // or // NSIndexPath *indexPath = [collectionview indexPathsForSelectedItems]; zoomVC.strId = indexPath.item;//indexPath.row; } } 

而你是呼叫button事件,创build一个共同的variables共享:

 @interface WebViewVC (){ int index; // you declare interface } @end - (void)collectionViewButtonPressed:(UIButton *)sender { index = sender.tag; [self performSegueWithIdentifier:@"venues" sender:self]; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"venues"]) { UIViewController *zoomVC = segue.destinationViewController; zoomVC.strId = index; } } 

在你的情况下,如果将创build一个新的类从UIButton派生自己的新的自定义标签来存储数据,同时生成表格单元格。 虽然“performsegue”可能检索您的使用这些数据。 我不会使用通常的标签属性,因为它在uitablecell uiview中有自己的用法。