functionios6jailbreaktheoslogos

Calling a function from another class with Logos (%hook)


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?


Solution

  • 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];