gpt4 book ai didi

ios - NSMutableArray 没有像预期的那样改变事件

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

我可以很好地循环遍历它们,但我想做的是更改背景颜色(对于 objective-c 来说仍然是新的)。这是我拥有的:

@interface MYViewController ()
@property (nonatomic, retain) NSMutableArray *listImages;
@end

@implementation MYViewController

- (void)viewDidLoad
{

self.listImages = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"bg-menuitem-highlighted.png"],
[UIImage imageNamed:@"bg-menuitem-highlighted.png"],
[UIImage imageNamed:@"bg-menuitem-highlighted.png"], nil];

[self.listImages enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
// do something with object
UIImage *image = [self.listImages objectAtIndex:idx];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.clipsToBounds = YES;
[self.view addSubview:imageView];
imageView.frame = CGRectMake(100*idx, 10*idx, imageView.frame.size.width + 15, imageView.frame.size.height + 15);

[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];
imageView.contentMode = UIViewContentModeCenter;

}];
[self.view addSubview:btnEventClick];
[super viewDidLoad];
}

- (void)myEventDo:(DoEvent *)btnEventClick {
NSLog(@"My event is doing!");
int i = 0;
for (NSString *item in self.listImages) {
NSLog(@"hi");
UIImage *newImage = [self.listImages objectAtIndex:i++];
UIImageView *newImageView = [[UIImageView alloc] initWithImage:newImage];
newImageView.backgroundColor = [UIColor redColor];
}
}

我做错了什么?

最佳答案

您在全新的 UIImageView 上设置了 backgroundColor,然后不显示它。就个人而言,当您创建和修改这些对象时,我会创建另一个 UIImageView 对象数组。像这样的东西:

@interface otViewController ()
@property (nonatomic, retain) NSMutableArray *listImages;
@property (nonatomic, retain) NSMutableArray *listImageViews;
@end

@implementation otViewController


- (void)viewDidLoad
{

self.listImages = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"S.png"],
[UIImage imageNamed:@"X.png"],
[UIImage imageNamed:@"O.png"], nil];
self.listImageViews = [[NSMutableArray alloc] initWithCapacity:[self.listImageViews count]];

[self.listImages enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
// do something with object
UIImage *image = [self.listImages objectAtIndex:idx];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.clipsToBounds = YES;
[self.view addSubview:imageView];
imageView.frame = CGRectMake(100*idx, 10*idx, imageView.frame.size.width + 15, imageView.frame.size.height + 15);

[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];
imageView.contentMode = UIViewContentModeCenter;
[self.listImageViews addObject:imageView];

}];

[super viewDidLoad];
}


- (IBAction)buttonClick:(id)sender {

NSLog(@"My event is doing!");

for (UIImageView *item in self.listImageViews) {
NSLog(@"hi");
item.backgroundColor = [UIColor redColor];
}

}

而且 rdelmar 是正确的,您的数组是 UIImage 对象,而不是 NSString 对象。

关于ios - NSMutableArray 没有像预期的那样改变事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20748683/

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