gpt4 book ai didi

ios - 从按钮操作调用 AppDelegate 中的方法时出现错误。

转载 作者:行者123 更新时间:2023-11-29 04:50:35 26 4
gpt4 key购买 nike

当我单击“保存”时,当它与 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;
}
}
}

最佳答案

您的代码看起来不错。所以我猜appDelegate实际上不是AppDelegate的实例。

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

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

关于ios - 从按钮操作调用 AppDelegate 中的方法时出现错误。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8916453/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com