2012-01-18 67 views
0

当我点击“保存”时,我收到一个错误,当它回到AppDelegate。从按钮操作调用AppDelegate中的方法时出错。

我敢肯定,我在这里错过了一些小东西,但如果任何人有一个快速的答案,将不胜感激。

这里是我得到的错误:

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

这里是我的按钮动作:

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

而且这里是我的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; 
    } 
} 
} 

回答

1

你的代码看起来OK。所以我猜appDelegate实际上不是AppDelegate的一个实例。

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

在那里设置一个断点,并在调试器中查看它是否确实是AppDelegate的一个实例。

+0

我想到了这一点,但它试图保存新的用户名(和作品)之前使用appDelegate抓取存储的用户标识。这就是为什么它让我如此困惑。 – TheTC 2012-01-18 20:26:40

+0

我清理了我的项目,然后重建。工作得很好。很奇怪。 – TheTC 2012-01-18 20:28:07