【发布时间】:2011-05-22 21:52:09
【问题描述】:
在我的应用程序中,我想在开始滚动 UITableView 时隐藏键盘。我在互联网上搜索这个,大多数答案是子类化 UITableView (http://stackoverflow.com/questions/3499810/tapping-a-uiscrollview-to-hide-the-keyboard)。
我做了子类,但它不起作用。
#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 中不起作用
.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];
【问题讨论】:
标签: ios uitableview keyboard scroll