ios - Objective C IOS prepareForSegue 如何获取分配给下一个 View Controller 按钮的 int 变量

标签 ios objective-c segue

所以我有一个 Collection View ,它使用核心数据动态生成一组按钮。我已经完成了为按钮分配按钮和值的部分。

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

现在这段代码在这里,我如何获取单元格/按钮中的 sportsValue 将其传递到下一个 View Controller ,以便我可以将其用作变量

- (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 中-

#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;
    }
}

YourNextVIewController.h类中添加此行

@property (strong, nonatomic) NSNumber *passNumber;

现在您可以在YourNextVIewController.m中检查passSportsValue

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

希望这能解决您的问题..

关于ios - Objective C IOS prepareForSegue 如何获取分配给下一个 View Controller 按钮的 int 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37740552/

相关文章:

ios - 日历的 UICollectionView 中有多少个单元格开始? (即具有无限数量的日期)

iphone - XCode 的内存泄漏仪器时间线中的歧义

objective-c - 为什么在 iPhone 中从服务器发送和接收 JSON 数据这么慢?

iphone - 使用 UIScrollView 从上到下滚动

ios - 如果条件为 True,则执行 segue

iphone - 用户将 NSNumbers 输入变量的好方法

ios - 3D touch peek and pop 隐藏导航栏和状态栏

iphone - 在注释点击上传递值 - iphone google map SDK

objective-c - TabBar躲起来了,怎么问都不想回来……

iOS objective-c 执行选择器不执行该方法