目标C IOS准备ForSegue如何将分配给按钮的整型变量I获取到Next View控制器 [英] Objective C IOS prepareForSegue how do I get an int variable I assigned to a button to next View controller

查看:19
本文介绍了目标C IOS准备ForSegue如何将分配给按钮的整型变量I获取到Next 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以将其传递给下一个视图控制器,以便将其用作变量

- (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中sports值并传递给--的示例

FirstViewController.mclass-

#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 class中添加此行

@property (strong, nonatomic) NSNumber *passNumber;

现在您可以签入passSportsValueYourNextVIewController.m

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

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

这篇关于目标C IOS准备ForSegue如何将分配给按钮的整型变量I获取到Next View控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆