问题
So I have a collection view which generates a set of buttons dynamically using core data. I have completed that part where I have assigned buttons and values to the buttons.
- (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;
}
So this works fine
Now this is the code perform Segue
- (void)collectionViewButtonPressed:(UIButton *)sender {
[self performSegueWithIdentifier:@"venues" sender:self];
}
Now this code here 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
- (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
}
}
回答1:
Here is an example how you can get that sportsValue in the collectionViewButtonPressed and pass to nextVC-
In 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]; // i.e. your sportsValue
/* if you are using indexPath.row i.e. 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;
}
}
In YourNextVIewController.h class add this line
@property (strong, nonatomic) NSNumber *passNumber;
Now you can check passSportsValue in YourNextVIewController.m
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%d",[self.passNumber integerValue]);
}
Hope this will solved your problem..
回答2:
First set a tag property to each button equal to required index
and then in selector method you can obtain that index
- (void)collectionViewButtonPressed:(UIButton *)sender {
NSInteger index = sender.tag;
[self performSegueWithIdentifier:@"venues" sender:index];
}
回答3:
Please this is only logic:
- (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;
}
}
And you are call button event that create one common variable to share:
@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;
}
}
回答4:
In your case if would create a new class derived from UIButton with a your own new custom tag to store the data while table cell is being generating. While 'performsegue' it is possible to retrieve this data for your usage. I would not use usual tag property due to it has its own usage in uitablecell uiview.
来源:https://stackoverflow.com/questions/37740552/objective-c-ios-prepareforsegue-how-do-i-get-an-int-variable-i-assigned-to-a-but