2009-11-06 5 views
0

Ok j'ai donc ce codeiPhone UIAlertView Problème

NSMutableArray *smartThings = [[NSMutableArray alloc] initWithCapacity:3]; 
NSMutableString *smartString = [NSString alloc]; 
int random; 
int numOfThings = 1; 
random = arc4random() % numOfThings; 
smartString = [smartThings objectAtIndex:random]; 
UIAlertView *smartAlert = [[UIAlertView alloc] initWithTitle:@"Thing To Say" message:smartString delegate:self cancelButtonTitle:@"Thanks" otherButtonTitles:nil]; 
[smartAlert show]; 
[smartAlert release]; 

[smartThings release]; 

Qu'est-ce qu'il « devrait » faire est de créer une UIAlertView sur une chaîne, et cette chaîne est créée à partir d'un tableau de chaînes.

Lorsque je l'exécute et que j'appuie sur le bouton pour appeler ce code, le programme se bloque. S'il vous plaît aider

Et oui j'ai branché toutes les connexions dans IB.

Répondre

0

Ok, je l'ai corrigé cette fois. Je l'avais dans le fichier MainView.m au lieu du fichier MainViewController.m.

3

smartThings est vide tout au long de l'exécution de cet extrait, même si vous avez l'espace pré-alloué pour 3 articles, afin -objectAtIndex soulèveront une NSRangeException, qui échouerait le programme sinon pris.

De plus, puisque smartThing est uniquement destiné à être un pointeur vers un objet à l'intérieur smartThings, vous ne devriez pas alloc il, déclarant simplement comme NSString* suffit.

1

Il semble que vous avez quelques problèmes:

  1. Il n'y a pas de chaînes ajoutées à smartThings, donc il n'y a rien pour smartString à pointer.
  2. smartString devrait probablement être déclaré comme un NSString *, NSMutableString est pour les chaînes dont vous voulez changer le contenu.
  3. numOfThings est totalement redondant. Utilisez [longueur smartThings] pour savoir combien de chaînes sont dans smartThings
0

Ok, je pense que je l'ai réparé. voici mon code

NSMutableArray *smartThings = [[NSMutableArray alloc] initWithObjects:@"It's still intact, just in lots of little pieces.",nil]; 
    NSMutableString *smartString; 
    int random; 
    int numOfThings = 1; 
    random = arc4random() % numOfThings; 
    smartString = [smartThings objectAtIndex:random]; 
    UIAlertView *smartAlert = [[UIAlertView alloc] initWithTitle:@"Smart Thing" message:smartString delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil]; 
    [smartAlert show]; 
    [smartAlert release]; 
    [smartThings release]; 

Mais lorsque j'appuie sur le bouton pour appeler ce code, le simulateur se bloque.