gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:30:51 25 4
gpt4 key购买 nike

所以我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com