我在代码中名为_dict的NSMutableArray中存储了5张图像。在 .h 文件中:
@property(weak,nonatomic)IBOutlet UIButton *b1;
@property(weak,nonatomic)IBOutlet UIButton *b2;
@property(weak,nonatomic)IBOutlet UIButton *b3;
@property(weak,nonatomic)IBOutlet UIButton *b4;
@property(weak,nonatomic)IBOutlet UIButton *b5;
@property(weak,nonatomic)IBOutlet UIButton *b6;
@property(weak,nonatomic)IBOutlet UIButton *b7;
@property(weak,nonatomic)IBOutlet UIButton *b8;
@property(weak,nonatomic)IBOutlet UIButton *b9;
在.m 文件中
dict=[[NSMutableArray alloc]init];
dict= _array;
NSLog(@"%@",dict);
colorimage = [dict objectAtIndex:0];
[_b1 setBackgroundImage:colorimage forState:UIControlStateNormal];
colorimage1 = [dict objectAtIndex:1];
[_b2 setBackgroundImage:colorimage1 forState:UIControlStateNormal];
colorimage2 = [dict objectAtIndex:2];
[_b3 setBackgroundImage:colorimage2 forState:UIControlStateNormal];
colorimage3 = [dict objectAtIndex:3];
[_b4 setBackgroundImage:colorimage3 forState:UIControlStateNormal];
colorimage4 = [dict objectAtIndex:4];
[_b5 setBackgroundImage:colorimage4 forState:UIControlStateNormal];
int j=0;
img=[[NSMutableArray alloc]init];
}
-(IBAction)button1:(id)sender{
k++;
[img addObject:colorimage];
[sender setBackgroundImage:[UIImage imageNamed:@"apple.png"] forState:UIControlStateNormal];
[self check];
}
-(IBAction)button2:(id)sender{
k++;
[img addObject:colorimage1];
[sender setBackgroundImage:[UIImage imageNamed:@"apple.png"] forState:UIControlStateNormal];
[self check];
}
-(IBAction)button3:(id)sender
{
k++;
[img addObject:colorimage2];
[sender setBackgroundImage:[UIImage imageNamed:@"apple.png"] forState:UIControlStateNormal];
[self check];}
-(IBAction)button4:(id)sender
{
k++;
[img addObject:colorimage3];
[sender setBackgroundImage:[UIImage imageNamed:@"apple.png"] forState:UIControlStateNormal];
[self check];
}
-(IBAction)button5:(id)sender
{
k++;
[img addObject:colorimage4];
[sender setBackgroundImage:[UIImage imageNamed:@"apple.png"] forState:UIControlStateNormal];
[self check];
}
-(IBAction)button6:(id)sender
{
}
-(IBAction)button7:(id)sender
{
}
-(IBAction)button8:(id)sender
{
}
-(IBAction)button9:(id)sender
{
}
-(void)check{
if(k==5)
{
// NSArray *arr1 = [[NSArray alloc]initWithObjects:@"aa",@"bb",@"1",@"cc", nil];
// NSArray *arr2 = [[NSArray alloc]initWithObjects:@"aa",@"bb",@"1",@"cc", nil];
if([dict isEqualToArray:img])
{
NSLog(@"equal");
UIAlertController * alert=[UIAlertController alertControllerWithTitle:@"Title"
message:@"Message"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* Retry = [UIAlertAction actionWithTitle:@"you got"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(@"you pressed Yes, please button");
// call method whatever u need
}];
[alert addAction:Retry];
[self presentViewController:alert animated:YES completion:nil];
}
else{
NSLog(@"not equal........");
UIAlertController * alert=[UIAlertController alertControllerWithTitle:@"Title"
message:@"Message"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* Retry = [UIAlertAction actionWithTitle:@"please try again............"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(@"you pressed Yes, please button");
// call method whatever u need
}];
[alert addAction:Retry];
[self presentViewController:alert animated:YES completion:nil];
}
}
// else
// {
// UIAlertController * alert=[UIAlertController alertControllerWithTitle:@"Title"
// message:@"Message"
// preferredStyle:UIAlertControllerStyleAlert];
// UIAlertAction* Retry = [UIAlertAction actionWithTitle:@"please try again"
// style:UIAlertActionStyleDefault
// handler:^(UIAlertAction * action)
// {
// NSLog(@"you pressed Yes, please button");
// call method whatever u need
// }];
// [alert addAction:Retry];
// [self presentViewController:alert animated:YES completion:nil];
// }
}
我得到了图像。但是正如我所说,我已经在dict中存储了5张图像,我需要显示UIbutton的图像随机标记。但没有得到。
而且我还在NSMutableArray中存储了10张图像。我需要从数组中随机选择 4 张图像,并需要显示未填充的 uibutton img 中的图像(即......已经有 5 张图像将显示在 UIButton 的不同标签上,剩余的 4 张 UIButton 应该显示存储在 10 张图像数组中的图像)。怎么办?
使用IBOutletCollection的好方法是使用IBOutletCollection。与其创建这么多的出口,你可以使用IBOutletCollection并使你的代码变小。当你这样做的时候,你可以把所有的9个按钮收集在一个数组中。
@IBOutlet var buttons: [UIButton]!
现在让我们假设您生成了随机标签 - 使用一些随机生成器 - 并存储在其中:
var randomTags: [Int]
现在您可以使用这个漂亮的 Swift 语法来过滤和获取新的按钮数组,该数组仅包含那些标签随机生成并存储在 randomTags 中的按钮
let newButtons = buttons.filter { (button) -> Bool in
return randomTags.contains(button.tag)
}
希望对您有所帮助。