一定要抓住所有! - UIView over UITableView 不接收点击
这个应用程序的问题是 TapCatchingController 没有接收到水龙头 位于节标题上或节之间。我想了解我应该如何做到这一点。 我试图让我的示例程序尽可能小,但它仍然相当长, 对此感到抱歉。然而,该代码是独立的。如果您将其复制并粘贴到您的 AppDelegate,你应该可以开始了! :)
提前致谢, - 缺口
#import <UIKit/UIKit.h>
//-----------------------------------------------------------------------------------------------
//Class InfiniteViewController: Says something when view is tapped, view is invisible to the user
@interface TapCatchingViewController : UIViewController {} @end
@implementation TapCatchingViewController
- (void) loadView {
self.view = [[UIView alloc] init];
self.view.alpha = 1.0;
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"You tapped this! Tuhm-dudu-dum"); }
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; }
- (void) viewWillDisappear:(BOOL)animated { [super viewWillDisappear: animated]; }
@end
//-----------------------------------------------------------------------------------------------
//Class SuperViewControoler: A tableview who has on top of it an InfiniteViewController's view
//Any tap on a SuperViewController's view should pass through the InfiniteViewController
@interface SuperViewController : UITableViewController {
TapCatchingViewController * vc;
}
@end
static const int sections = 7;
static const int rowsPerSection = 9;
@implementation SuperViewController
- (void) viewDidLoad {
vc = [[TapCatchingViewController alloc] init];
vc.view.frame = CGRectInfinite;
[self.view addSubview: vc.view];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return sections; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return rowsPerSection; }
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return @"Section Header"; }
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 250.0f; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = @"Meow, meow.";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}
- (void)dealloc {
[vc dealloc];
[super dealloc];
}
@end
//-----------------------------------------------------------------------------------------------
//Class AppDelegate! The usual stuff :)
@interface InfiniviewsAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UIViewController * vc;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
@implementation InfiniviewsAppDelegate
@synthesize window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
vc = [[SuperViewController alloc] initWithStyle: UITableViewStyleGrouped];
[window addSubview: vc.view];
[window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {}
- (void)applicationDidBecomeActive:(UIApplication *)application {}
- (void)applicationWillTerminate:(UIApplication *)application {}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {}
- (void)dealloc {
[vc release];
[window release];
[super dealloc];
}
@end
The problem with this app is that the TapCatchingController does not receive the taps
that are on section headers or between sections. I'd like to learn how I should do that.
I have tried to keep my sample program as small as possible, but it's still rather long,
sorry for that. The code is however self contained. If you copy and paste it in your
AppDelegate, you should be good to go! :)
Thanks in advance,
--Nick
#import <UIKit/UIKit.h>
//-----------------------------------------------------------------------------------------------
//Class InfiniteViewController: Says something when view is tapped, view is invisible to the user
@interface TapCatchingViewController : UIViewController {} @end
@implementation TapCatchingViewController
- (void) loadView {
self.view = [[UIView alloc] init];
self.view.alpha = 1.0;
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"You tapped this! Tuhm-dudu-dum"); }
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; }
- (void) viewWillDisappear:(BOOL)animated { [super viewWillDisappear: animated]; }
@end
//-----------------------------------------------------------------------------------------------
//Class SuperViewControoler: A tableview who has on top of it an InfiniteViewController's view
//Any tap on a SuperViewController's view should pass through the InfiniteViewController
@interface SuperViewController : UITableViewController {
TapCatchingViewController * vc;
}
@end
static const int sections = 7;
static const int rowsPerSection = 9;
@implementation SuperViewController
- (void) viewDidLoad {
vc = [[TapCatchingViewController alloc] init];
vc.view.frame = CGRectInfinite;
[self.view addSubview: vc.view];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return sections; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return rowsPerSection; }
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return @"Section Header"; }
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 250.0f; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = @"Meow, meow.";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}
- (void)dealloc {
[vc dealloc];
[super dealloc];
}
@end
//-----------------------------------------------------------------------------------------------
//Class AppDelegate! The usual stuff :)
@interface InfiniviewsAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UIViewController * vc;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
@implementation InfiniviewsAppDelegate
@synthesize window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
vc = [[SuperViewController alloc] initWithStyle: UITableViewStyleGrouped];
[window addSubview: vc.view];
[window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {}
- (void)applicationDidBecomeActive:(UIApplication *)application {}
- (void)applicationWillTerminate:(UIApplication *)application {}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {}
- (void)dealloc {
[vc release];
[window release];
[super dealloc];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我是否理解你的问题,但是
我能看到的唯一问题是 viewController 的视图添加了
[self.view addSubview: vc.view];
但不能保证它将始终位于窗口顶部可能缺少的代码行是 < code>[self.view BringSubviewToFront:vc.view];
但是,您可以通过对 vc.view 应用不同的颜色来检查 vc.view 是否位于顶部或底部/code>
但我还是无法理解你的目标为什么要在
TableViewController
子类中添加视图控制器的视图希望它对你有帮助
I am not sure that I am understanding your problem or not but
Only Problem I can see is viewController's view is added with
[self.view addSubview: vc.view];
but there is no guarantee that it will be always on top of windowLine of code might missing is
[self.view bringSubviewToFront:vc.view];
However you can check that
vc.view
is on top or bot by by applying different colour tovc.view
Still I am not able to understand your goal why you are adding view controller's view in
TableViewController
subclassHope it is helpful to you