你有什麼理解錯誤。 UIBarButtonItem
從不是發件人,因爲它的類是NSObject
而不是UIView
。 UIBarButtonItem
不能接收觸摸,當我們點擊一個酒吧項目時接收觸摸是一個UINavigationButton
(私人類)。
如果您希望在用戶點擊視圖時記錄每次用戶交互,我爲您提供了一個解決方案。
- 創建一個
UIWindow
(調用CustomWindow)的子類。
- 覆蓋
sendEvent
方法UIWindow
捕捉用戶交互,檢查並記錄裏面的交互。
didFinishLaunchingWithOptions
AppDelegate
的方法,用一個窗口替換默認window
類型爲CustomWindow
。
這是我CustomWindow
登錄與UIControl
類的控制事件UIControlEventTouchUpInSide
用戶交互。
@implementation CustomWindow
- (void)sendEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
switch ([touch phase]) {
case UITouchPhaseBegan:
{
NSLog(@"Touch Began");
}
break;
case UITouchPhaseMoved:
NSLog(@"Touch Move");
break;
case UITouchPhaseEnded:
{
NSLog(@"Touch End");
// Check if touch ends inside view (UIControlEventTouchUpInSide)
if (CGRectContainsPoint(CGRectMake(0, 0, touch.view.frame.size.width, touch.view.frame.size.height), [touch locationInView:touch.view])) {
// Only log info of UIControl objects
if ([touch.view isKindOfClass:[UIControl class]]) {
[self logViewInfo:(UIControl *)touch.view];
}
}
}
break;
case UITouchPhaseCancelled:
NSLog(@"Touch Cancelled");
break;
default:
break;
}
[super sendEvent:event];
}
- (void)logViewInfo:(UIControl *)clickedButton {
NSString *frontMostViewcontrollerClassName = NSStringFromClass(self.topViewController.class);
NSString *targetClassName = NSStringFromClass([[clickedButton.allTargets anyObject] class]);
NSString *selectorName =[[clickedButton actionsForTarget:[clickedButton.allTargets anyObject] forControlEvent:UIControlEventTouchUpInside] firstObject];
NSString *senderClassName = NSStringFromClass(clickedButton.class);
NSLog(@"UI Front-most view controller: %@\nInteraction Action [%@ %@] by sender %@ control event UIControlEventTouchUpInSide", frontMostViewcontrollerClassName, targetClassName, selectorName, senderClassName);
}
- (UIViewController*)topViewController {
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)viewController {
if ([viewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)viewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([viewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navContObj = (UINavigationController*)viewController;
return [self topViewControllerWithRootViewController:navContObj.visibleViewController];
} else if (viewController.presentedViewController && !viewController.presentedViewController.isBeingDismissed) {
UIViewController* presentedViewController = viewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
}
else {
for (UIView *view in [viewController.view subviews])
{
id subViewController = [view nextResponder];
if (subViewController && [subViewController isKindOfClass:[UIViewController class]])
{
if ([(UIViewController *)subViewController presentedViewController] && ![subViewController presentedViewController].isBeingDismissed) {
return [self topViewControllerWithRootViewController:[(UIViewController *)subViewController presentedViewController]];
}
}
}
return viewController;
}
}
更多的細節,你可以檢查我的演示回購here
嗨@trungduc,感謝您的幫助 - 我將盡快嘗試它。 – Wayne