我有一个静态void,它创建一个带动作的按钮,我希望动作也是一个静态的void,但当我尝试制作包时,我得到一个错误。
代码:
typedef enum {
SBIconLocationHomeScreen = 0,
SBIconLocationDock = 1,
SBIconLocationSwithcer = 2
} SBIconLocation;
static UIButton *okbtn;
@interface SBApplicationIcon
- (void)launchFromLocation:(SBIconLocation)location context:(id)arg2;
@end
static void AddBtn()
{
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
okbtn = [UIButton buttonWithType:UIButtonTypeCustom];
[okbtn addTarget:window action:@selector(checkpw:) forControlEvents:UIControlEventTouchUpInside];
[okbtn setTitle:@"Done" forState:UIControlStateNormal];
[okbtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
okbtn.frame = CGRectMake(0, 0, 160, 40);
okbtn.translatesAutoresizingMaskIntoConstraints = NO;
[UIView transitionWithView:window duration:0.4 options:UIViewAnimationOptionTransitionCrossDissolve animations: ^ {
[window addSubview:okbtn];
} completion:nil];
[window addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|-255-[okbtn(40)]"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(okbtn)]];
[window addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:[okbtn(160)]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(okbtn)]];
[window addConstraint:[NSLayoutConstraint
constraintWithItem:okbtn attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:window attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
}
static void checkpw()
{
//Something
}
%hook SBApplicationIcon
- (void)launchFromLocation:(SBIconLocation)location context:(id)arg2
{
AddBtn();
}
%end
错误:
Tweak.xm:43:13: error: unused function 'checkpw' [-Werror,-Wunused-function]
static void checkpw()
我该如何解决?
答案 0 :(得分:0)
问题在于这两部分代码:
[okbtn addTarget:window action:@selector(checkpw:) forControlEvents:UIControlEventTouchUpInside];
// …
static void checkpw()
{
//Something
}
您使用普通的C方法作为选择器。选择器仅适用于Objective-C方法;你需要创建一个Objective-C类并将方法添加到它:
[okbtn addTarget:[[PasswordChecker alloc] init] action:@selector(checkpw:) forControlEvents:UIControlEventTouchUpInside];
// …
@implementation PasswordChecker
- (void)checkpw:(NSString *)password {
// Something
}
@end