2010-03-18 8 views
2

J'ai besoin de changer les images sur une matrice de UIButtons, et la seule chose que je connaisse pour adresser les boutons, c'est la balise. Mais je ne peux pas trouver un moyen de réellement nous cet identifiant. Les boutons sont créés par programmation pendant viewDidLoad.Comment accéder à un UIButton en utilisant un tag et changer son image?

Voici le code pour créer des boutons:

#define N_ROWS 4 
#define N_COLS 3 

    int N_IMG = 0; 
    for (int a = 0; a < N_COLS; a++) { 
     for (int j = 0; j < N_ROWS; j++) { 


      UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
      aButton.frame = CGRectMake(a * 65.0 + 25, j * 65.0 + 15, 10.0, 10.0); 
      aButton.tag = j + a * N_ROWS + 1; 
      [aButton setBackgroundColor:[UIColor redColor]]; 

      N_IMG = N_IMG++; 
      [self.view addSubview:aButton]; 
      number_sorted = 1; 

     } 
    } 

Voici le code pour définir l'image:

- (IBAction)set_image:(id)sender { 

    #define N_ROWS 4 
    #define N_COLS 3 

     int N_IMG = 0; 
     for (int a = 0; a < N_COLS; a++) { 
      for (int j = 0; j < N_ROWS; j++) { 
       uibutton aButton.tag == (j + a * N_ROWS + 1) 
       setImage:[UIImage imageNamed:[puzzles objectAtIndex:N_IMG]] 
          forState:UIControlStateNormal]; 
      N_IMG = N_IMG++; 

      } 
     } 
    } 

Voici le code où commence truble: UIButton aButton.tag == (j + a * N_ROWS + 1)

Qui puis-je configurer pour que cela fonctionne?

Répondre

4

Je ne comprends pas vraiment ce que vous essayez de faire, mais pourquoi ne pouvez-vous pas stocker vos objets UIButton (par exemple dans un objet NSArray), de sorte que vous pouvez y accéder plus tard (dans votre deuxième boucle)?

8

Réponse courte:

RE: "Comment accéder à un UIButton utilisant une balise ..."

UIButton *tmpButton = (UIButton *)[self.view viewWithTag:tmpTag]; 

RE: "... et changer son image"

[tmpButton setImage:[UIImage imageNamed:@"MyGreatImage.png"] forState:UIControlStateNormal]; 

.

Réponse longue:

RE: * "Ceci est le code où commence truble: UIButton aButton.tag == (j + a * N_ROWS + 1)" *

Oui, je peut voir le problème. La ligne que vous utilisez est pour réglage l'étiquette d'un bouton, pas pour obtenir le bouton de le tag.

Pour obtenir un bouton d'une étiquette connue faire:

// define the tag index 
int tmpTag = 123;//replace "123" with your own logic, i.e. (j + a * N_ROWS + 1) 

// get the button with the given tag 
UIButton *tmpButton = (UIButton *)[self.view viewWithTag:tmpTag]; 

// assign the image 
tmpImage = [UIImage imageNamed:@"MyGreatImage.png"]; 
[tmpButton setImage:tmpImage forState:UIControlStateNormal]; 

CODE BONUS: A ce stade, vous pouvez également ajouter ou supprimer des actions associées à votre bouton.

//Remove all actions associated the button 
[aButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents]; 

//Assign a new button action: using the exact selector name ("myMethodName") 
[aButton addTarget:self action:@selector(myMethodName:) forControlEvents:UIControlEventTouchUpInside]; 

//Assign a new button action: using a calculated selector name 
//(suppose I have a bunch of methods with the prefix "myNumberedMethodName_" followed by an index. 
int tmpSomeNumber = 12; 
SEL tmpSelector = NSSelectorFromString ([NSString stringWithFormat:@"myNumberedMethodName_%i:",tmpSomeNumber); 
// don't forget to include the ":" symbol in the selector name 
[aButton addTarget:self action:tmpSelector forControlEvents:UIControlEventTouchUpInside]; 

REMARQUE: "viewWithTag" renvoie normalement un objet View. Un bouton est un type spécial de vue avec des propriétés spéciales telles que l'image. Donc pour l'avoir renvoyé un objet Button au lieu de l'objet View plus générique, nous l'initialisons comme un bouton en le convertissant en utilisant (UIButton *) dans la définition.

Mais si tout ce que vous voulez est de changer l'opacité du bouton, vous n'avez pas besoin de le transformer en bouton. Vous pouvez simplement l'initialiser comme vue générique:

// set opacity of a button 
float tmpOpacity = 0.5;//half-visible 
UIView *tmpView = [self.view viewWithTag:tmpTag];//get the view object associated with button's tag (remember, a button IS a view) 
[[tmpView layer] setOpacity:tmpOpacity]; 

Voir aussi Button with Tag.