如何从文本字段中获取更改文本

时间:2010-09-29 13:21:33

标签: iphone sdk

我有自定义单元格的tableview。单元格有2个textfields.from viewcontroller我需要在该textfield中找到更改的文本。

如果我使用

* (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

此方法不是在调用mainviewcontroller调用。

所以我发送这样的通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(CalculateAmountInCartList:) name:UITextFieldTextDidChangeNotification object:nil];

在这里我得到了textfiled文本被更改。但是我没有控制特定单元格的文本字段和索引,我也没有。

请提出任何建议。

谢谢, 勒凯什

3 个答案:

答案 0 :(得分:0)

你可以使用标签来做到这一点,不确定这是最好的方式..

在viewDidLoad方法中向textfield添加标记..

textField.tag = 1; //整数#1,10,99等


(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {    if(textField.tag == 1){    }    if(textField.tag == 2){    } }

答案 1 :(得分:0)

您可以使用您知道的NSNotificationCenter发布消息。

- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo

所以你可以在NSDictionary中粘贴你想要的任何东西。

答案 2 :(得分:0)

我建议你尝试确定哪个字段是第一个响应者;这里有示例代码Get the current first responder without using a private API,解释如何执行此操作。

然后我还会将一个标签与每个编辑字段相关联,这样你就知道表格中的哪一行与编辑字段相关联,类似于@KiranThorat所建议的

相关问题