gpt4 book ai didi

iphone - 如何检测包含目标操作(对应于按钮单击)的 "viewcontroller"文件?

转载 作者:行者123 更新时间:2023-11-29 04:02:20 26 4
gpt4 key购买 nike

在一个ios项目中,有无数的viewcontroller文件,开发了我很多人,经过一段时间。我启动了模拟器,并导航到各个屏幕。在导航中间,我想找到当在屏幕上按下按钮时将调用哪个操作方法。如何轻松找到它,无需过多分析项目,也无需使用断点。

最佳答案

创建 UIButton 类别并确保它包含在您的目标中:

UIButton+actionsFinder.h

#import <UIKit/UIKit.h>

@interface UIButton (actionsFinder)

@end

UIButton+actionsFinder.m

#import "UIButton+actionsFinder.h"

@implementation UIButton (actionsFinder)


-(void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
NSLog(@"%s %d %s %@ %@\n %@\n %@", __FILE__, __LINE__, __PRETTY_FUNCTION__, @"Button clicked!\n", NSStringFromSelector(action), [target description], [event description]);
[super sendAction:action to:target forEvent:event];
}

@end

当任何按钮发送任何操作时,您将在控制台看到类似以下内容:

/Users/username/appname/targetname/UIButton+actionsFinder.m 15 -[UIButton(actionsFinder) sendAction:to:forEvent:] Button clicked!

onButton:

<MCViewController: 0x715e280>

<UITouchesEvent: 0x7639f40> timestamp: 23516.9 touches: {( <UITouch: 0x7169240> phase: Ended tap count: 1 window: <UIWindow: 0x754e6d0; frame = (0 0; 320 568); autoresize = W+H; layer = <UIWindowLayer: 0x754e7d0>> view: <UIRoundedRectButton: 0x7161290; frame = (123.5 38; 73 44); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x71613b0>> location in window: {149, 81.5} previous location in window: {149, 81.5} location in view: {25.5, 23.5} previous location in view: {25.5, 23.5}

请注意,您将获得以下信息:

  • 目标类 onButton: 中我声明的选择器名称并链接到按钮 onTouchUpInside 事件操作,实际方法签名看起来像

    -(IBAction)onButton:(id)button;

  • 显示目标类的目标描述:MCViewController

  • 触发操作的事件的描述

我使用 __FILE__、__LINE____PRETTY_FUNCTION__ 宏来查看 NSLog 消息的来源。

关于iphone - 如何检测包含目标操作(对应于按钮单击)的 "viewcontroller"文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15705759/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com