简体   繁体   English

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

[英]Textfield value not being updated in time for save textFieldDidEndEditing

I'm working on a simple app that uses Core Data. 我正在开发一个使用Core Data的简单应用程序。 I have a detail screen with a textfield that I want to have saved when I click a done button. 单击“完成”按钮时,我有一个要保存的带有文本字段的详细信息屏幕。 Right now, the done action is being completed before textFieldDidEndEditing is being called. 目前,已完成的操作在调用textFieldDidEndEditing之前已完成。

(Using xcode 6) (使用xcode 6)

I can get the field input logged to the console. 我可以将字段输入记录到控制台。 so I think I have the outlets set up correctly and the delegate relationships in place. 所以我认为我已经正确设置了渠道,并建立了代理关系。

Here's what I believe to be the relevant code: 我认为这是相关的代码:

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

As you can see from this log, I'm getting the message from the done action before the textfieldDidEndEditing call: 从该日志中可以看到,在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 

Where is my problem? 我的问题在哪里? Thanks. 谢谢。

Edited: I tried resignFirstResponder at top of done method with no impact. 编辑:我尝试在完成的方法顶部resignFirstResponder,没有影响。 Also, I tried setting season.seasonName = self.seasonNameTextField.text with no change. 另外,我尝试将season.seasonName = self.seasonNameTextField.text设置为不变。

Another edit: If I use this code either with or without the self.seasonName = newText I see that the characters are being entered in the text field but self.seasonNameTextField.text remains null when I try to retrieve it. 另一个编辑:如果无论是否使用self.seasonName = newText都使用此代码,我会看到在文本字段中输入了字符,但是当我尝试检索它时self.seasonNameTextField.text仍然为null。

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

Edited: More log info 编辑:更多日志信息

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

Edited: Here is my code in AppDelegate to create the MOC and pass it to the SeasonVC and the code that passes it to the SeasonFactsVC. 编辑:这是我在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;
    }
}

See this answer -- https://stackoverflow.com/a/9101699/3096507 查看此答案-https: //stackoverflow.com/a/9101699/3096507

Basically textFieldDidEndEditing is called when the keyboard is dismissed. 基本上,关闭键盘时会调用textFieldDidEndEditing Which probably isn't until after you click the done button. 直到您单击“完成”按钮之后,这才可能出现。

So you can either check your textField in your done method or use - (BOOL)textFieldShouldReturn:(UITextField *)textField instead. 因此,您可以在done方法中检查textField,或改用- (BOOL)textFieldShouldReturn:(UITextField *)textField

Looking at this the text field is being resigned as part of you dismissing the view controller at the end of the done method. 看着这个,在完成方法的最后,解雇视图控制器是文本字段的一部分。 Not before. 没过 If you want to get the textfields text sooner you could link the field as an outlet and get the text value from that Ie self.mytextfield.text either that or call resignFirstResponder on the text field at the top of the done method. 如果您想早点获取textfields文本,则可以将该字段链接为出口,并从该文本即ie self.mytextfield.text获取文本值,或者在done方法顶部的文本字段上调用resignFirstResponder。

EDIT: 编辑:

create an outlet for your textfield (and link it to your textfield in interface builder): 为您的文本字段创建一个出口(并将其链接到界面构建器中的文本字段):

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

then update your done method 然后更新您的完成方法

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

You loose the Focus of your TextField AFTER you clicked on a new UI Control. 单击新的UI控件后,您将释放TextField的Focus。 So you will always have your Button function first called before the DidEndEdition will fire. 因此,在DidEndEdition触发之前,始终将始终先调用Button函数。

You could use 你可以用

textViewDidChange

to update your seasonName 更新您的seasonName

- (void) textFieldDidEndEditing:(UITextField *)textField gets called after your text field resign first responder which is after you click the save button so there is no problem with that. - (void) textFieldDidEndEditing:(UITextField *)textField在您的文本字段退出第一响应者(即单击“保存”按钮后)后被调用,因此没有问题。 If you have the text field outlet why not directly check that and assign value? 如果您有文本字段出口,为什么不直接检查并分配值呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 保存由textFieldDidEndEditing更改的textfield值是否不会保存在核心数据中? - Saving textfield value altered by textFieldDidEndEditing won't save in core data? 什么时候会调用textFieldDidEndEditing:textField? - When Will textFieldDidEndEditing:textField be called? 再次调用textFieldDidEndEditing时,NSFilemanager不会保存文件 - NSFilemanager doesn't save file when textFieldDidEndEditing is called a second time 为什么 textFieldDidEndEditing: 没有被调用? - Why is textFieldDidEndEditing: not being called? 如何将文本字段更改通知合并到textFieldDidEndEditing中 - How to incorporate textfield change notification into textFieldDidEndEditing 单击按钮修改文本字段,不调用textFieldDidEndEditing吗? - Click button to amend textfield, textFieldDidEndEditing not called? Firebase值未更新? - Firebase value not being updated? textFieldDidEndEditing与didSelectRowAtIndexPath,后来没有被调用 - textFieldDidEndEditing vs didSelectRowAtIndexPath, the later not being called 如何从 textFieldDidEndEditing 获取数据以保存到 Firebase? - How to get data from textFieldDidEndEditing to save to Firebase? 保存UITextField测试的最佳方法:textFieldShouldReturn或textFieldDidEndEditing - Best method to save UITextField test: textFieldShouldReturn or textFieldDidEndEditing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM