iOSプロジェクトには、一定期間にわたって多くの人々が開発した多数のviewcontrollerファイルがあります。シミュレーターを起動し、さまざまな画面に移動しました。ナビゲーションの途中で、画面でボタンが押されたときに呼び出されるアクション メソッドを見つけたい。プロジェクトをあまり分析せず、ブレークポイントを使用せずに簡単に見つける方法。
2 に答える
1
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 メッセージの送信元を確認していました。
于 2013-03-29T15:18:55.170 に答える
0
ボタンを右クリックして、以下を表示できます。
特定のタッチイベントに追加されるセレクター。
またはNSLog(@"%s", __PRETTY_FUNCTION__);
、関数ごとに印刷して、その関数が呼び出されるたびにこのログに関数名を出力します。
于 2013-03-29T14:59:49.607 に答える