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

时间:2014-10-08 21:40:00

标签: ios core-data uitextfield

我正在使用一个使用Core Data的简单应用。我有一个带有文本字段的详细信息屏幕,当我点击完成按钮时,我希望保存该文本字段。现在,在调用textFieldDidEndEditing之前完成了done操作。

(使用xcode 6)

我可以将字段输入记录到控制台。所以我认为我的网点设置正确,代理关系到位。

以下是我认为的相关代码:

- (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调用之前收到了来自done操作的消息:

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 

我的问题在哪里?感谢。

编辑:我在完成方法的顶部尝试了resignFirstResponder而没有任何影响。另外,我尝试设置season.seasonName = self.seasonNameTextField.text,没有任何变化。

另一个编辑:如果我在使用或不使用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;
}

已编辑:更多日志信息

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

4 个答案:

答案 0 :(得分:1)

请参阅此回答 - https://stackoverflow.com/a/9101699/3096507

键盘解除时,基本上会调用textFieldDidEndEditing。在您单击完成按钮之后可能不会这样。

因此,您可以使用done方法检查textField,也可以使用- (BOOL)textFieldShouldReturn:(UITextField *)textField

答案 1 :(得分:1)

查看此文本字段正在作为您在done方法结束时关闭视图控制器的一部分进行重新签名。不是之前。 如果你想尽快获得文本字段文本,你可以将字段作为插座链接,并从那个Ie self.mytextfield.text获取文本值,或者在done方法顶部的文本字段上调用resignFirstResponder。

修改

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

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

然后更新你的完成方法

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

答案 2 :(得分:0)

单击新的UI控件后,松开TextField的焦点。因此,在DidEndEdition将触发之前,您将始终首先调用Button函数。

您可以使用

textViewDidChange

更新你的季节名

答案 3 :(得分:0)

- (void) textFieldDidEndEditing:(UITextField *)textField在您的文本字段重新签名第一个响应者之后被调用,这是在您单击保存按钮之后所以没有问题。如果你有文本字段出口,为什么不直接检查并分配值?