0

我如何sent events从一个地方的 UITextField 收集信息,或者我是否被迫为我打算使用的每个事件创建出口和操作?

在此处输入图像描述

4

2 回答 2

1

如果我正确地回答了您的问题,则当此 UITextField 失去焦点(用户在其他地方点击)时,您需要从 UITextField 获取文本。为此,您需要:

  1. 将您的班级声明为(在 yourClassName.h 文件中)
  2. 在 yourClassName.m 文件中实现这个方法:

    - (void)textFieldDidEndEditing:(UITextField *)textField {
        NSString *someStringOrWhateverYouNeed = textField.text;   
    }
    

任何时候用户按下键盘上的返回按钮,您的班级都会收到通知并调用此方法。

如果您需要从多个 UITextField 收集事件,您可以使用特定标签标记所有 textField 并创建一个 IBAction,如下所示:

- (IBAction)getTextFieldEvent:(id)sender {
    UITextField *currentTextField = (UITextField *)sender;
    switch (currentTextField.tag) {
        case 1:
            // some code here for textField with tag = 1
            break;
        case 2:
            // some code here for textField with tag = 2
            break;
        case 3:
            // some code here for textField with tag = 3
            break;
        default:
            // some default code here
            break;
    }
}

对于不同的事件类型,我可以建议创建不同的 IBAction。如果您不需要更改 UITextField 的属性(例如字体等),那么您实际上不需要 IBOutlets。

希望有帮助:)

于 2012-04-29T04:56:55.030 回答
0

使用相同的名称进行另一个 IBAction 连接。然后擦除重复的方法。两个文本字段都将连接到相同的 IBAction 方法。

于 2012-12-18T21:28:14.480 回答