gpt4 book ai didi

ios - 试图从 didSelectCellAtIndex 将图片加载到新的 ViewController

转载 作者:行者123 更新时间:2023-11-29 01:51:22 24 4
gpt4 key购买 nike

我有一个 CollectionView,在 didSelectItemAtIndexPath 方法中我实例化了一个新的 ViewController,将 ViewController 上的 String 属性设置为保存图像的名称。然后在新 ViewController 中的 viewDidLoad 或 viewDidAppear 方法中,我尝试使用 vc.imageName 属性设置图像。

这是代码:

// collection.m


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
ViewController *vc = [[ViewController alloc] init];

NSString *imageName = [colouringPhotos objectAtIndex:indexPath.row];
vc.imageName = imageName;
[self presentViewController:vc animated:YES completion:nil];
}


// ViewController.h

@property (nonatomic, strong) NSString * imageName;

//ViewController.m

@property (strong, nonatomic) IBOutlet UIImageView *lineImageView;

-(void) viewDidAppear:(BOOL)animated{
self.lineImageView.image = [UIImage imageNamed:self.imageName];
}

但图像不显示。我已经在 ImageView 上设置了背景颜色,以检查它是否显示在新的 ViewController 中,我可以看到它,但图像永远不会显示在 UIImageView 中。

最佳答案

您可以使用 Segue 来显示下一个 View Controller 。假设您使用“ShowNextViewController”作为 Segue 的标识符,您可以使用 prepareForSegue:sender:设置imageName目标 View Controller 的属性:

- (void)prepareForSegue:(nonnull UIStoryboardSegue *)segue sender:(nullable id)sender {

if ([segue.identifier isEqualToString:@"ShowNextViewController"]) {
ViewController *destination = (ViewController *)segue.destinationViewController;
NSIndexPath *selectedIndex = [[self.collectionView indexPathsForSelectedItems] firstObject];
destination.imageName = self.colouringPhotos[selectedIndex.row];
}
}

如果您在设置 segue 时遇到问题,请参阅 this iOS Developer Library document了解更多信息。

另见 UIViewController Class Reference有关 prepareForSegue:sender: 的更多信息方法。

关于ios - 试图从 didSelectCellAtIndex 将图片加载到新的 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31370723/

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