2012-01-18 1 views
0

Lorsque je clique sur "enregistrer", j'obtiens une erreur quand il répond à AppDelegate. Je suis sûr, qu'il me manque quelque chose de petit ici, mais si quelqu'un a une réponse rapide, il serait grandement apprécié.Erreur lors de l'appel de la méthode dans AppDelegate à partir d'une action de bouton.

est ici l'erreur que je reçois:

-[AppDelegate saveNewUsername:username:]: unrecognized selector sent to instance 0x8e403d0 

Voici mon action bouton:

-(IBAction)saveButtonPressed:(id)sender 
{ 
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

NSString *userid = [appDelegate retrieveFromUserDefaults:@"userid"]; 
NSMutableArray *currentUser = [appDelegate getUserInfo:userid]; 
NSString *currentUsername = [currentUser objectAtIndex:0]; 

NSLog(@"%@", userid); 

if ([currUsername.text isEqualToString:currentUsername]) { 
    //here we'll check and make sure the new username is valid and doesn't already exist (for now just update local DB) 
    if ([theNewUsername.text isEqualToString:@""]) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"You must enter something for your new username." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
     [alert show]; 
    } else { 
     int returnVal = [appDelegate saveNewUsername:userid username:theNewUsername.text]; 
     if (returnVal == 1) { 
      [self dismissModalViewControllerAnimated:YES]; 
     } else { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"There was an error saving to the database." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
      [alert show]; 
     } 
    } 
} else { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"The username you entered is not your current username." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
    [alert show]; 
} 
} 

Et voici ma méthode AppDelegate:

-(int)saveNewUsername:(NSString *)user_id username:(NSString *)username 
{ 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"somedbname.sqlite3"]; 

FMDatabase *db = [FMDatabase databaseWithPath:writableDBPath]; 

if (![db open]) { 
    NSLog(@"Could not open db."); 
    return 0; 
} else { 
    [db intForQuery:@"UPDATE user SET username=? WHERE user_id=?", username, user_id]; 

    if ([db hadError]) { 
     NSLog(@"Error %d: %@", [db lastErrorCode], [db lastErrorMessage]); 
     return 0; 
    } else { 
     return 1; 
    } 
} 
} 

Répondre

1

Votre code semble OK. Donc je suppose que appDelegate n'est pas une instance de AppDelegate.

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

Définir un point de jonction et un coup d'oeil dans le débogueur pour vérifier si elle est en effet une instance de AppDelegate.

+0

J'ai pensé à cela, mais il utilise appDelegate pour saisir l'ID utilisateur stocké avant qu'il ne tente de sauvegarder le nouveau nom d'utilisateur (et fonctionne). C'est pourquoi il m'a tellement confus. – TheTC

+0

J'ai nettoyé mon projet, puis l'ai reconstruit. Fonctionne très bien. Très étrange. – TheTC

Questions connexes