在查看弹出窗口之前存储UITextField内容 [英] Storing UITextField contents before view pops

查看:150
本文介绍了在查看弹出窗口之前存储UITextField内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信这是在Apple文档中,或者必须在此论坛的某个地方得到解答,因为它看起来很基本,但我自己也找不到它,也不是一个特别优雅的解决方案。

I am sure this is in the Apple documentation or must have been answered somewhere on this forum, since it seems so basic, but I could not find it nor a particularly elegant solution myself.

我所拥有的是一个在其导航堆栈上推送编辑视图的UIViewController。编辑视图中包含一堆UITextField。如果在触摸后退按钮时正在编辑其中一个,则在 textFieldShouldEndEditing 或$ code> textFieldDidEndEditing的UITextField委托方法之前调用原始视图的ViewWillAppear方法调用或IB链接操作 textFieldEditingEnded 方法。

What I have is a UIViewController that pushes an editing view on its navigation stack. The editing view has a bunch of UITextFields in it. If one of them is being editing when the back button is touched, the original view's ViewWillAppear method is called before either the UITextField delegate methods of textFieldShouldEndEditing or textFieldDidEndEditing, or the IB linked action textFieldEditingEnded method are called.

这是一些代码我希望它会更清楚:

Here is some code that I hope will make it clearer:

在UIViewController中:

In the UIViewController:

- (void) viewWillAppear: (BOOL) animated {
    [super viewWillAppear: animated];
    NSLog( @"Entering view will appear for master view" );
    nameLabelField.text = objectToEdit.name;
}
- (IBAction) editMyObject: (id) sender {
    NSLog( @"Editing the object" );
    EditViewController *evc = [[EditViewController alloc] initWithNibName: @"EditTableView" bundle: nil];
    evc.editedObject = objectToEdit;
    [self.navigationController pushViewController: evc animated: YES];
    [evc release];
}

在EditViewController< UITextFieldDelegate>中:

In the EditViewController <UITextFieldDelegate>:

- (void) viewWillAppear: (BOOL) animated {
    [super viewWillAppear: animated];
    nameField.text = editedObject.name;
}
- (void) viewWillDisappear: (BOOL) animated {
    [super viewWillDisappear: animated];
    NSLog( @"In viewWillDisappear" );
    if( [self.navigationController.viewControllers indexOfObject: self] == NSNotFound ) {
        NSLog( @"-- We are not in controller stack... the back button has been pushed" );
    }
}
- (BOOL) textFieldShouldEndEditing: (UITextField *) textField {
    NSLog( @"In textFieldShouldEndEditing" );
    // Store text field value here???
    // editedObject.name = nameField.text;
    return YES;
}
- (void) textFieldDidEndEditing: (UITextField *) textField {
    NSLog( @"In textFieldDidEndEditing" );
    // Store text field value here???
    // editedObject.name = nameField.text;
}
- (IBAction) textFieldEditingEnded: (id) sender {
    NSLog( @"In textFieldEditingEnded" );
    // Store text field value here???
    // editedObject.name = nameField.text;
}

日志最终得到:


[...]进入视图将显示为主视图

[...]编辑对象

[... ]在viewWillDisappear中

[...] - 我们不在控制器堆栈中...后退按钮已被推送

[...]将出现为主人输入视图查看

[...]在textFieldShouldEndEditing中

[...]在textFieldEditingEnded中

[...]在textFieldDidEndEditing中

[...] Entering view will appear for master view
[...] Editing the object
[...] In viewWillDisappear
[...] -- We are not in controller stack... the back button has been pushed
[...] Entering view will appear for master view
[...] In textFieldShouldEndEditing
[...] In textFieldEditingEnded
[...] In textFieldDidEndEditing

我想在<$中设置标签之前设置 self.editedObject.name = nameField.text 对于UIViewController,c $ c> viewWillAppear 。

I want to set self.editedObject.name = nameField.text before the label gets set in viewWillAppear for the UIViewController.

我在viewWillDisappear方法中考虑过EditViewController,检查我的任何文本字段当前是否是第一个响应者,如果是,那么获取文本并存储它,但是如果我添加或更改文本字段,这似乎是一种难以维护的问题。

I thought about in the viewWillDisappear method for the EditViewController checking to see if any of my text fields are currently the first responder and if so getting their text and storing it, but this seems like such a kludge that will be a pain to maintain if I add or change text fields.

我还可以实现 textFieldEditingChanged IB链接操作,在每次击键后设置编辑对象中的文本但是这样也是相当多的开销,因为我必须找出每次击键时我在哪个文本字段(记住我只显示 name 但是有一大堆它们)。

I can also implement the textFieldEditingChanged IB linked action to set the text in the edited object after every keystroke but this is also quite a bit of overhead since I have to figure out which text field I am in every keystroke (remember I only showed name but there are a whole bunch of them).

我需要的是编辑结束或知道编辑将在UIViewController中调用viewWillAppear之前结束,因此正确设置了nameFieldLabel。

All I need is for the editing to be ended or to know the editing will be ended before viewWillAppear is called in the UIViewController so the nameFieldLabel is properly set.

推荐答案

好的,经过大量的网上冲浪,论坛阅读和手动阅读,我找到了一个简单的解决方案。我怀疑它非常简单,只添加了一行代码。在EditViewContorller的 viewWillDisappear 方法中,我只需添加:

OK, I figured out a simple solution after a lot of web-surfing, forum reading, and manual reading. It was, as I suspected, very simple, only one line of code added. In the viewWillDisappear method of the EditViewContorller I simply added:

    [self.view.window endEditing: YES];

现在 textFieldShouldEndEditing textFieldEditingEnded textFieldDidEndEditing 所有在主视图的 viewWillAppear 之前被解雇。

Now textFieldShouldEndEditing, textFieldEditingEnded, and textFieldDidEndEditing all get fired off before the viewWillAppear of the master view does.

现在 viewWillDisappear 方法如下所示:

- (void) viewWillDisappear: (BOOL) animated {
    [super viewWillDisappear: animated];
    NSLog( @"In viewWillDisappear" );
    // Force any text fields that might be being edited to end so the text is stored
    [self.view.window endEditing: YES];
}

还有处理键盘上返回的方法处理导航控制器上的后退按钮。

And the methods already in place to handle the 'Return' on the keyboard also handle the 'Back' button on the Navigation controller.

感谢Aaron和Jeff的帮助,并帮助我思考。

Thank you Aaron and Jeff for your assistance and helping me think this through.

这篇关于在查看弹出窗口之前存储UITextField内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆