2013-03-08 53 views
2

注:此代码是不是原始代码的翻版,但足以说明(具有良好的精度)是什么问题,我的代码的意图是什么。调用从另一个类的功能与标志​​(%钩)

我添加了一个按钮,DaClass1的看法(这工作得很好):

%hook DaClass1 

-(id)DaView { 
    UIButton *xButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [xButton addTarget:self action:@selector(dismissWithAnimation:YES:nil) 
     forControlEvents:UIControlEventTouchUpInside]; 
    [xButton setBackgroundImage:[UIImage imageWithContentsOfFile:@"/Hello.png"] forState:UIControlStateNormal]; 
    xButton.frame = CGRectMake(0, 0, 30, 30); 
    [self addSubview:xButton]; 
    return %orig; 
} 

%end 

UIButtonaction:dismissWithAnimation:YES:nil)实际上是从另一个类:

%hook DaClass2 
    -(void)dismissWithAnimation:(int) reason:(int) { 
     //someCodeHere... 
    } 
%end 

我怎么可以叫dismissWithAnimationDaClass2从我UIButtonaction:当UIButton的是DaClass1

回答

2

,您可以拨打%new函数调用DaClass2dismissWithAnimation

%hook DaClass1 

//Your Code... 

%new 

-(void)dismissIt { 
    [[%c(DaClass2) sharedInstance] dismissWithAnimation:YES:nil]; 
} 

%end 

,并设置xButtonaction:为 “dismissIt”:

[xButton addTarget:self action:@selector(dismissIt) forControlEvents:UIControlEventTouchUpInside]; 
1

你的意思是,@selector(dismissWithAnimation:是:无)方法是在类DaClass2?

然后做:

[xButton addTarget:(instance of DaClass2) action:@selector(dismissWithAnimation:YES:nil) forControlEvents:UIControlEventTouchUpInside]; 
+0

它是DaClass2的一个实例,除非它是一个类的方法。 – HackyStack 2013-03-08 21:45:06

+0

还是坚持DaClass2的一个实例的引用,声明局部按钮按下处理方法,在这种方法中,调用DaClass2对象的dismissWithAnimation方法。 – HackyStack 2013-03-08 21:48:22