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

UIButton's action:( dismissWithAnimation:YES:nil) 实际上来自另一个类:

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

当 UIButton 在时,我如何从我的 'sdismissWithAnimation中调用?DaClass2UIButtonaction:DaClass1

4

2 回答 2

2

您可以创建一个%new调用dismissWithAnimation.DaClass2

%hook DaClass1

//Your Code...

%new

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

%end

并将xButton's设置action:为“ dismissIt”:

[xButton addTarget:self action:@selector(dismissIt) forControlEvents:UIControlEventTouchUpInside];
于 2013-03-09T20:46:44.707 回答
1

您的意思是@selector(dismissWithAnimation:YES:nil) 方法在DaClass2 类中吗?

然后做:

[xButton addTarget:(instance of DaClass2) action:@selector(dismissWithAnimation:YES:nil) forControlEvents:UIControlEventTouchUpInside];
于 2013-03-08T21:28:52.173 回答