gpt4 book ai didi

iphone - 无法在字典中添加 Imageview 引用来查看

转载 作者:行者123 更新时间:2023-11-29 04:23:03 24 4
gpt4 key购买 nike

我不确定我是否正确执行此操作,但本质上我想在 Controller 加载时实例化我的 uiimageviews。之后,当我实现一个在计时器上执行的方法时,我想根据索引获取 uiimageview 的引用并将其添加到 View 中。

我正在使用 NSMutableDictionary。我将所有代码都放在同一个方法中只是为了测试它。不应该 [self.view addSubview:poster];将第一张图像放置在当前 View 中?

self.images = [NSMutableDictionary dictionary];

CGRect imageFrame = CGRectMake(0.0, 0.0, 1024.0, 280.0);


UIImageView *image_one = [[UIImageView alloc] initWithFrame:imageFrame];
image_one.image = [UIImage imageNamed:@"image_one.png"];
NSString *theKey = [NSString stringWithFormat:@"%d",0];
[self.images setObject:image_one forKey:theKey];





UIImageView *image_two = [[UIImageView alloc] initWithFrame:imageFrame];
image_two.image = [UIImage imageNamed:@"image_two.png"];
NSString *theKey1 = [NSString stringWithFormat:@"%d",1];
[self.images setObject:image_two forKey:theKey1];



UIImageView *poster = (UIImageView *)[self.images objectForKey:0];
[self.view addSubview:poster];

最佳答案

首先,我建议使用 NSMutableArray。由于无论如何您都是通过索引引用数组中的项目,因此不妨使用数组。

虽然使用此代码,但仍有几件事您应该更改。您应该分配 NSMutableArray 而不是使用自动释放的版本。

当您访问 UIImageView 时,键是一个字符串,而不是整数。所以,它应该是:

UIImageView *poster = (UIImageView *)[self.images objectForKey:@"0"];

关于iphone - 无法在字典中添加 Imageview 引用来查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12719672/

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