Вызов функции из другого класса с использованием Logos (%hook)
Примечание. Этот код не является точной копией исходного кода, но иллюстрирует (с хорошей точностью), в чем заключается проблема и каковы мои намерения в отношении кода.
Я добавил кнопку в 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
Как я могу позвонить dismissWithAnimation
в DaClass2
от моего UIButton
"s action:
когда UIButton находится в DaClass1
?
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];
Вы имеете в виду, что метод @selector(dismissWithAnimation:YES:nil) находится в классе DaClass2?
Затем сделайте:
[xButton addTarget:(instance of DaClass2) action:@selector(dismissWithAnimation:YES:nil) forControlEvents:UIControlEventTouchUpInside];