滚动 UITableView 时隐藏键盘 [英] Hide keyboard when scroll UITableView

查看:51
本文介绍了滚动 UITableView 时隐藏键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,当我开始滚动 UITableView 时,我想隐藏键盘.我在互联网上搜索了这个,大多数答案是继承 UITableView (http://stackoverflow.com/questions/3499810/tapping-a-uiscrollview-to-hide-the-keyboard).

In my app i want hide keyboard when i start scrolling UITableView. I search about this in internet, and most answer is subclassing UITableView (http://stackoverflow.com/questions/3499810/tapping-a-uiscrollview-to-hide-the-keyboard).

我创建了子类,但它不起作用.

I made subclass but it's dont work.

#import <UIKit/UIKit.h>

@protocol MyUITableViewDelegate <NSObject>
@optional
- (void)myUITableViewTouchesBegan;
@end

@interface MyUITableView : UITableView <UITableViewDelegate, UIScrollViewDelegate> {
    id<MyUITableViewDelegate> delegate;
}
@end

.m 文件

#import "MyUITableView.h"

@implementation MyUITableView

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"delegate scrollView"); //this is dont'work
    [super scrollViewDidScroll:scrollView];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"delegate myUITableViewTouchesBegan"); // work only here
    [delegate myUITableViewTouchesBegan];
    [super touchesBegan:touches withEvent:event];

}

- (void)dealloc {
...

我像这样使用这个类.但是委托函数 myUITableViewTouchesBegan 在 ViewController 中不起作用

I use this class like this. But delegate function myUITableViewTouchesBegan don't work in ViewController

.h

#import <UIKit/UIKit.h>
#import "MyUITableView.h"

@interface FirstViewController : UIViewController <UITableViewDelegate, UISearchBarDelegate, MyUITableViewDelegate> {
    MyUITableView *myTableView;
    UISearchBar *searchBar; 
}

@property(nonatomic,retain) IBOutlet MyUITableView *myTableView;
...

.m

- (void) myUITableViewTouchesBegan{
    NSLog(@"myUITableViewTouchesBegan");
    [searchBar resignFirstResponder];
}

我在这个实现上遇到了一些麻烦:
1) myUITableViewTouchesBegan 在 ViewController 中不起作用
2) 来自 MyUITableView.m 的 NSLog - NSLog(@"delegate myUITableViewTouchesBegan"); 仅在我触摸表时工作.当我开始滚动时,它是如何工作的?
我尝试覆盖 scrollViewDidScroll 但编译器说 MyUITableVIew 可能不响应此字符串 [super scrollViewDidScroll:scrollView];

I have some troubles with this implemenation:
1) myUITableViewTouchesBegan dont work in ViewController
2) NSLog from MyUITableView.m - NSLog(@"delegate myUITableViewTouchesBegan"); work only when i touch table. How made it's work also when i start scrolling?
I try override scrollViewDidScroll but comiler said that MyUITableVIew may be don't respond on this string [super scrollViewDidScroll:scrollView];

推荐答案

不知道为什么你需要子类化 UITableView .

Not sure why you need to subclass UITableView for this.

在包含普通 UITableView 的视图控制器中,尝试添加:

In the view controller that contains the plain UITableView, try adding this:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [searchBar resignFirstResponder];
}

这篇关于滚动 UITableView 时隐藏键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆