gpt4 book ai didi

ios - 文本字段值未及时更新以保存 textFieldDidEndEditing

转载 作者:行者123 更新时间:2023-11-28 21:56:48 28 4
gpt4 key购买 nike

我正在开发一个使用核心数据的简单应用程序。我有一个带有文本字段的详细信息屏幕,当我单击完成按钮时我想保存它。现在,已完成的操作在调用 textFieldDidEndEditing 之前完成。

(使用 xcode 6)

我可以将字段输入记录到控制台。所以我想我已经正确设置了导出,并且委托(delegate)关系就位。

这是我认为相关的代码:

- (void) textFieldDidEndEditing:(UITextField *)textField
{
self.seasonName = textField.text;
NSLog(@"Did End Editing: %@", self.seasonName);
}

- (IBAction)done:(UIBarButtonItem *)sender {

Season *season = nil;

if (self.seasonToEdit) {
season = self.seasonToEdit;
} else {
season = [NSEntityDescription insertNewObjectForEntityForName:@"Season" inManagedObjectContext:self.managedObjectContext];
}

season.seasonName = self.seasonName;
//season.seasonDescription = self.seasonDescriptionTextView.text;

NSLog(@"Season name: %@", season.seasonName);

[self dismissViewControllerAnimated:YES completion:nil];
}

正如您从该日志中看到的,我在 textfieldDidEndEditing 调用之前从已完成的操作中获取消息:

2014-10-08 16:36:10.318 ScorerCD[12277:475893] Season name: (null)
2014-10-08 16:36:10.842 ScorerCD[12277:475893] Did End Editing: the

我的问题在哪里?谢谢。

已编辑:我在 done 方法顶部尝试了 resignFirstResponder,但没有任何影响。另外,我尝试设置 season.seasonName = self.seasonNameTextField.text 没有改变。

另一个编辑:如果我在有或没有 self.seasonName = newText 的情况下使用此代码,我会看到字符正在文本字段中输入,但 self.seasonNameTextField.text 在我尝试检索它时仍然为空。

- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newText = [theTextField.text stringByReplacingCharactersInRange:range withString:string];

self.doneBarButton.enabled = ([newText length] > 0);

NSLog(@"In change characters");

self.seasonName = newText;

return YES;
}

已编辑:更多日志信息

2014-10-09 09:46:33.280 ScorerCD[13179:517301] New Season
2014-10-09 09:46:37.788 ScorerCD[13179:517301] In change characters
2014-10-09 09:46:37.792 ScorerCD[13179:517301] In change characters
2014-10-09 09:46:42.104 ScorerCD[13179:517301] In change characters
2014-10-09 09:46:42.343 ScorerCD[13179:517301] In change characters
2014-10-09 09:46:42.627 ScorerCD[13179:517301] In change characters
2014-10-09 09:46:45.112 ScorerCD[13179:517301] Season to edit: (null)
2014-10-09 09:46:45.194 ScorerCD[13179:517301] Season: <Season: 0x7be3b7d0> (entity: Season; id: 0x7be323c0 <x-coredata:///Season/t7CBA137F-F348-47E6-A0EA-2518DAF422392> ; data: {
games = nil;
seasonDescription = nil;
seasonName = nil;
})
2014-10-09 09:46:45.194 ScorerCD[13179:517301] Season name: (null)
2014-10-09 09:46:45.712 ScorerCD[13179:517301] In change characters
2014-10-09 09:46:45.744 ScorerCD[13179:517301] Did End Editing: the high

已编辑:这是我在 AppDelegate 中创建 MOC 并将其传递给 SeasonVC 的代码,以及将其传递给 SeasonFactsVC 的代码。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
SeasonsViewController *controller = navigationController.viewControllers[0];
controller.managedObjectContext = self.managedObjectContext;
return YES;
}

- (NSManagedObjectContext *)managedObjectContext {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
if (_managedObjectContext != nil) {
return _managedObjectContext;
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"AddSeason"]) {
UINavigationController *navigationController = segue.destinationViewController;
SeasonFactsViewController *controller = (SeasonFactsViewController *) navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
controller.seasonToEdit = nil;
}
}

最佳答案

看着这个文本字段被辞退作为你在 done 方法结束时解散 View Controller 的一部分。之前没有。如果您想更快地获取文本字段文本,您可以将该字段链接为导出并从中获取文本值,即 self.mytextfield.text 或者在完成方法顶部的文本字段上调用 ​​resignFirstResponder。

编辑:

为您的文本字段创建一个 socket (并将其链接到界面构建器中的文本字段):

@property (nonatomic, weak) IBOutlet UITextfield *seasonNameTextField;

然后更新你的done方法

- (IBAction)done:(UIBarButtonItem *)sender {

Season *season = nil;

if (self.seasonToEdit != nil) {
season = self.seasonToEdit;
} else {
season = [NSEntityDescription insertNewObjectForEntityForName:@"Season" inManagedObjectContext:self.managedObjectContext];
}

season.seasonName = self.seasonNameTextField.text;

[self dismissViewControllerAnimated:YES completion:nil];
}

关于ios - 文本字段值未及时更新以保存 textFieldDidEndEditing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26266936/

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