Ios 当“下一步”按钮将Segue调用到下一个场景时,保存UITextField数据(用户输入)

Ios 当“下一步”按钮将Segue调用到下一个场景时,保存UITextField数据(用户输入),ios,uitextfield,segue,Ios,Uitextfield,Segue,损失6 我在一个场景中有6个UITextFields,其中有一个“下一步”按钮,可以切换到下一个场景 当我按下键盘上的“完成”按钮时,下面的代码非常有效: - (IBAction)dismissKeyboard:(id)sender { if (sender == LocationName) { self.meeting.LocationName = LocationName.text; } if (sender == LocationAddress) {

损失6

我在一个场景中有6个UITextFields,其中有一个“下一步”按钮,可以切换到下一个场景

当我按下键盘上的“完成”按钮时,下面的代码非常有效:

- (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调用的键盘是可见的,然后调用dismisskyboard并将发送者作为参数传递


感谢

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

nextButtonPressed:
的实现应该只需在下一个文本字段上调用
becomeFirstResponder
。就这样。通过使另一个文本字段成为第一个响应者,前一个文本字段将调用其
textfielddendediting:
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
    }
}

谢谢你的回复。dismissKeyboard:当我右键单击UITextField时,是否调用事件“退出时结束”,已发送事件-退出时结束是否链接到dismissKeyboard。这不是和textfielddidediting一样吗?此外,对于“您的nextButtonPressed:实现应该只调用becomeFirstResponder,不管下一个文本字段是什么。就是这样。通过使另一个文本字段成为第一个响应者,前一个文本字段将调用其TextFieldDiEndEditing:委托。”您能提供一个示例代码吗?谢谢你,这两天我都忙于考试。谢谢你更新答案。我相信,你在代码中的下一个按钮就是键盘上显示的下一个按钮。我指的是导航栏上的“下一步”按钮。“导航栏下一步”按钮将调用下一个场景。