2

Note: This code is not an exact replica of the original code, but illustrates (with good accuracy) what the issue is, and what my intentions with the code are.

I have added a button to DaClass1's view (this works fine):

%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

But the UIButton's action: (dismissWithAnimation:YES:nil) is actually from another class:

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

How may I call dismissWithAnimation in DaClass2 from my UIButton's action: when the UIButton is in DaClass1?

Aleksander Azizi
  • 9,829
  • 9
  • 59
  • 87

2 Answers2

2

You can make a %new function that calls dismissWithAnimation in DaClass2.

%hook DaClass1

//Your Code...

%new

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

%end

and set the xButton's action: to "dismissIt":

[xButton addTarget:self action:@selector(dismissIt) forControlEvents:UIControlEventTouchUpInside];
Aleksander Azizi
  • 9,829
  • 9
  • 59
  • 87
1

Do you mean that the @selector(dismissWithAnimation:YES:nil) method is in the class DaClass2?

Then do:

[xButton addTarget:(instance of DaClass2) action:@selector(dismissWithAnimation:YES:nil) forControlEvents:UIControlEventTouchUpInside];
HackyStack
  • 4,887
  • 3
  • 22
  • 28
  • It has to be an INSTANCE of DaClass2 unless it's a class method. – HackyStack Mar 08 '13 at 21:45
  • Or hold a reference to an instance of DaClass2, declare a local button press handler method, and in that method, call your dismissWithAnimation method on that DaClass2 object. – HackyStack Mar 08 '13 at 21:48