Tony.C Tony.C - 5 months ago 46
iOS Question

Objective C IOS prepareForSegue how do I get an int variable I assigned to a button to next View controller

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

}
}

Answer

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..