0

I have stored 5 images in an NSMutableArray named as _dict in the code. In .h file:

   @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;

In.m file

  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];


 //  }
}

i got the images.But as i told ,i have stored 5 images in dict.i need to display the images randomly tag of UIbutton.I tried with many code.But not got.

And also i have stored 10 images in NSMutableArray .And i need to select randomly 4 images from the array and need to display the image where unfilled uibutton img(that is...already 5 images will display on different tag of UIButtons and remaining 4 UIButton should display the images which is stored in array of 10 images).how to do?

teena
  • 41
  • 8

1 Answers1

0

The good way to use is to go for IBOutletCollection. Instead of creating so many outlets you can use IBOutletCollection and make your code small. When you do that you can collect all your 9 buttons inside one array.

@IBOutlet var buttons: [UIButton]!

Now let's assume you have random tags generated - use some random generator - and stored inside:

var randomTags: [Int]

Now you can use this beautiful Swift syntax to filter and get new buttons array which has only those buttons whose tag is randomly generated and stored inside randomTags

  let newButtons =   buttons.filter { (button) -> Bool in
            return randomTags.contains(button.tag)            
        }

Hope it helps.

manismku
  • 2,160
  • 14
  • 24
  • i need in objective-c – teena Oct 09 '17 at 04:50
  • You can use something like: NSArray *filteredArray = [buttons filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id button, NSDictionary *bindings) { return [randomTags containsObject: button.tag]; }]]; – manismku Oct 09 '17 at 04:57
  • how to give in .h file – teena Oct 09 '17 at 05:34
  • You don't have to use this on header file. You need to use it in implementation file (.m) and probably in viewDidload or any other method which you want to call at run time. – manismku Oct 09 '17 at 05:40
  • showing an error as buttons is not declare .what code should give – teena Oct 09 '17 at 06:25
  • how to declare all 9 buttons inside one array. – teena Oct 09 '17 at 06:27
  • You can follow this link to create collection: https://stackoverflow.com/questions/15836930/how-can-i-use-iboutletcollection-to-connect-multiple-uiimageviews-to-the-same-ou – manismku Oct 09 '17 at 06:40
  • can u give step by steps.According to that code is for uiimageview. – teena Oct 09 '17 at 06:58