IOS UIScrollview不允许键盘在触摸屏幕时关闭
问题描述:
我构建了一个应用程序,当textview被填充时,键盘会弹出并允许您键入。如果我决定不想使用键盘,我可以点击背景视图控制器,它会掉下来。现在,我添加了一个滚动视图。一切工作正常,但我无法获得连接的操作,并在触摸Textview外部时使键盘掉落。 (我不能在Xcode中制作自定义视图控制器)IOS UIScrollview不允许键盘在触摸屏幕时关闭
有没有人有任何修复?
预先感谢您!
@interface ViewController()
@property (weak, nonatomic) IBOutlet UITextField *actionField;
@property (weak, nonatomic) IBOutlet UITextField *impactField;
@property (weak, nonatomic) IBOutlet UITextField *resultField;
@end
@implementation ViewController
- (NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager]
fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc]
initWithContentsOfFile:filePath];
for (int i = 0; i < 3; i++) {
UITextField *theField = self.lineFields [i];
theField.text = array [i];
}
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification
object:app];
}
- (void)applicationWillResignActive:
(NSNotification *)notification
{
NSString *filePath = [self
dataFilePath];
NSArray *array = [self.lineFields valueForKey:@"text"];
[array writeToFile:filePath atomically:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)backgroundTap: (id)sender {
[self.actionField resignFirstResponder];
[self.impactField resignFirstResponder];
[self.resultField resignFirstResponder];
}
答
尝试UITapGestureRecognizer
:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapScrollView:)];
[scrollView addGestureRecognizer:singleTap];
和行动:
- (void)tapScrollView:(UITapGestureRecognizer *)gesture {
[self.actionField resignFirstResponder];
[self.impactField resignFirstResponder];
[self.resultField resignFirstResponder];
}
您可以发布您的代码? – Raptor 2015-03-03 04:03:15
感谢您寻找猛禽。我没有添加代码,因为我认为这是一个Xcode连接问题 – mhinton11 2015-03-03 05:16:13
据我所知,touchbegan函数在滚动视图中不起作用。每当用户滚动时都会更好,您可以关闭键盘。否则用键盘添加一个工具栏按钮(关闭)。 – 2015-03-03 05:35:00