gpt4 book ai didi

ios - 当“下一步”按钮调用转入下一个场景时保存 UITextField 数据(用户输入)

转载 作者:行者123 更新时间:2023-11-29 04:06:44 24 4
gpt4 key购买 nike

iOS6

我在一个场景中有 6 个 UITextField,并带有一个可以转入下一个场景的按钮。

当我点击键盘上的“完成”按钮时,下面的代码效果很好:

- (IBAction)dismissKeyboard:(id)sender
{
if (sender == LocationName) {
self.meeting.LocationName = LocationName.text;
}

if (sender == LocationAddress) {
self.meeting.LocationAddress = LocationAddress.text;
}

if (sender == LocationCity) {
self.meeting.LocationCity = LocationCity.text;
}

//I have 3 more text fields after and then I persist to CoreData:

NSError *error = nil;
if (![managedObjectContext save:&error]) {
}

[sender endEditing:YES];
}

如果用户点击键盘上的“完成”按钮,数据将保存良好。

但是,如果用户单击导航栏上的“下一步”按钮,而没有先单击键盘上的“完成”按钮,则用户在 UITextfield 中键入的内容不会保存。我希望当用户点击导航栏上的“下一步”按钮调用下一个场景时,用户在所有字段中键入的数据(用户从键盘输入的数据)将被保存。

我有以下用于“下一步”按钮的代码:

- (IBAction)nextButtonPressed:(id)sender {

[self dismissKeyboard:sender];
}

我知道 nextButtonPressed 的代码是错误的。我想我需要帮助来识别哪个 UITextField 调用键盘可见,然后调用解雇键盘并将发送者作为参数传递。

谢谢

最佳答案

利用 UITextField 委托(delegate)方法 textFieldDidEndEditing: 了解焦点何时离开文本字段。此时您应该保存其数据。当焦点移动到另一个文本字段或键盘完全关闭时,将调用此函数。

您的 nextButtonPressed: 实现应该简单地对下一个文本字段是什么调用 becomeFirstResponder 。就是这样。通过将另一个文本字段设置为第一响应者,前一个文本字段将调用其 textFieldDidEndEditing: 委托(delegate)。

更新:

// This assumes the LocationXXX are instance variables referencing the UITextFields
- (IBAction)nextButtonPressed:(id)sender {
if (sender == LocationName) {
[LocationAddress becomeFirstResponder];
} else if (sender == LocationAddress) {
[LocationCity becomeFirstResponder];
// add the rest as needed
}
}

关于ios - 当“下一步”按钮调用转入下一个场景时保存 UITextField 数据(用户输入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15074262/

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