我有一个文本字段,当我按Enter键时,我的文本字段字符串将发送到textView。每次我在文本字段中输入新行时,它都会使用该字符串并将其附加到textView中。
我需要做的是能够连续键入并按Enter键,将其追加到我的textView中,而无需重新选择textField
在viewDidLoad;
- (void)setupFloatTextBox {
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
UITextField *noteTextField = [[UITextField alloc] initWithFrame:CGRectMake(5, screenHeight - 55, screenWidth - 5, 30)];
noteTextField.backgroundColor = Rbg2UIColor(51, 182, 124);
[noteTextField.layer setBorderWidth:0.5f];
[noteTextField setAlpha:0.5f];
[noteTextField addTarget:self action:@selector(postJSON:) forControlEvents:UIControlEventEditingDidEnd];
[self.navigationController.view addSubview:noteTextField];
}
和我的选择器动作
- (void)postJSON:(UITextField *)sender {
if (sender.text.length > 0) {
FireStormClasses *fs = [[FireStormClasses alloc] init];
fs.CATEGORY = @"TASK";
fs.TASK = [sender.text substringWithRange:NSMakeRange(3, sender.text.length - 3)];
fs.WHO = [[sender.text substringToIndex:2] uppercaseString];
fs.PRIORITY = @"0";
fs.ACTION = @"UPDATED";
fs.HIDDEN = @"0";
fs.UDATE = [FireStorm getCurrentDateWithFormat:@"yyyy-MM-dd"];
fs.DUEDATE = [FireStorm getCurrentDateWithFormat:@"yyyy-MM-dd"];
fs.COMPLETEDATE = [FireStorm getCurrentDateWithFormat:@"yyyy-MM-dd"];
fs.LISTDATE = [FireStorm getCurrentDateWithFormat:@"yyyy-MM-dd"];
NSURL *URL = [NSURL URLWithString:@"myurl"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[manager POST:URL.absoluteString
parameters:[fs postCreateJSON]
progress:nil
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"%@", responseObject);
[self loadJSONData];
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@", [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding]);
NSLog(@"%@", error);
}];
sender.text = @"";
[sender resignFirstResponder];
[sender becomeFirstResponder];
} else {
NSLog(@"Texfield empty");
}
}
我正在使用的
sender resignFirstResponder];
和[sender becomeFirstResponder];
行使我的文本字段处于活动状态/非活动状态。我遇到的问题是,在第一种类型/输入时,我被带回到我的文本字段,在这里我可以继续输入,但是第二次按下Enter键时,该按钮不起作用。
heres a video explaining a bit better(很抱歉导致崩溃,请跳到20秒)
最佳答案
而不是将目标添加到文本字段,只需设置该文本字段的委托,并在UITextfield集合的委托方法中进行设置,
-(void)textFieldDidEndEditing:(UITextField *)textField
{
[textField becomeFirstResponder];
}
它将解决您的问题
关于ios - 如何在endEditing后以编程方式转到textField,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35444337/