我在UITableViewCell
当我直接点击文本字段时,会调用此方法:
-(void)editingChanged:(UITextField *)sender
这是cellForRowAtIndexPath
[cell.ProductQuantityTextField addTarget:self action:@selector(editingChanged:)forControlEvents:UIControlEventEditingChanged];
-(void) editingChanged:(UITextField *)sender
{
NSLog( @"text changed: %@",cell.ProductQuantityTextField.text);
[self ProdcutDirectSetToCartAPICall];
}
我想要的是,当我点击文本字段然后我更改textfield的值然后我点击键盘的完成按钮那时我想要调用UITextField
方法
答案 0 :(得分:2)
您正在寻找shouldReturn
委托方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// do something
return YES;
}
答案 1 :(得分:1)
有两种方法可以做到这一点
1。)您可以执行UIControlEventEditingDidEndOnExit事件。
2。)或者你也可以在文本字段retun
中实现你的方法<UITextfieldDelegate> //in yourController.h file
//yourController.m
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//do your code here.
return YES;
}
这两种方法都可以帮助您解决问题。
答案 2 :(得分:0)
我认为每次更改forControlEvents:UIControlEventEditingChanged
内的某些内容时都会调用UITextField
。如果您想在用户点击键盘上的'Done'
按钮时调用该方法,请尝试更改行
[cell.ProductQuantityTextField addTarget:self action:@selector(editingChanged:)forControlEvents:UIControlEventEditingChanged];
带
[cell.ProductQuantityTextField addTarget:self action:@selector(editingChanged:)UIControlEventEditingDidEnd];
希望它有所帮助。
答案 3 :(得分:0)
您可以查看UItextfield的应该返回方法调用
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
static void handler(int sig, siginfo_t* si, void *unused){
if(sig == SIGINT){
printf("Signal %i received\n",si->si_signo);
}
}
int main(int argc, char* argv[]){
char s [256];
struct sigaction sa = {0};
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, SIGINT);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
if(sigaction(SIGINT, &sa, NULL) < 0 ){
perror("sigaction");
}
char *p;
do {
p = fgets(s,sizeof(s), stdin);
} while (!p && errno == EINTR);
printf("%s\n", s);
return 0;
}
答案 4 :(得分:0)
在您的班级中实施<UITextFieldDelegate>
协议,设置
ProductQuantityTextField.delegate = self;
并使用
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
NSLog(@"Textfield text %@", textField.text);
[self ProdcutDirectSetToCartAPICall];
}
或
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
答案 5 :(得分:0)
我检查了以下代码,其工作正常
在cellForRowAtIndexPath中我添加了以下代码
cell.ProductQuantityTextField.delegate = self;
cell.ProductQuantityTextField.tag = indexPath.row; //If you want you can give tag here
[cell.ProductQuantityTextField addTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
-(void)editingChanged:(UITextField *)field
{
NSLog( @"text changed: %@",field.text);
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog( @"textFieldShouldReturn: %@",textField.text);
[textField resignFirstResponder];
//If you need cell
TableViewCellName *cell = (TableViewCellName*) [[textField superview]superview];
NSLog( @"text changed: %@",cell.ProductQuantityTextField.text);
[self ProdcutDirectSetToCartAPICall];
return YES;
}