繁体   English   中英

在objective-c中的UIButton的标签上显示数据

[英]display the data on the tag of UIButton in objective-c

我在代码中名为_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;

In.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张图像将显示在UIButtons的不同标签上,其余4张UIButton应该显示存储在10个图像数组中的图像。怎么办?

使用的好方法是去IBOutletCollection 您可以使用IBOutletCollection来简化代码,而不必创建太多的出口。 这样做时,您可以将所有9个按钮收集在一个阵列中。

@IBOutlet var buttons: [UIButton]!

现在,假设您生成了随机标签-使用一些随机生成器-并存储在其中:

var randomTags: [Int]

现在您可以使用这种漂亮的Swift语法来过滤并获取新的button数组,该数组仅包含那些标签是随机生成并存储在randomTags中的按钮

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

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM