在尝试将选择器作为方法的参数调用时,将无法识别的选择器发送到类

时间:2017-03-22 07:43:39

标签: ios objective-c

我正在尝试将视图控制器中存在的当前工作代码放到帮助程序类中。但是将“无法识别的选择器发送到类”错误。

上一个视图控制器:

UIImage *buttonImg = [[UIImage imageNamed:@"img-settings"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIButton *buttonSetting = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonSetting setTintColor:[ColorHelper hiltiDirtyPurple]];
[buttonSetting setImage:buttonImg forState:UIControlStateNormal];
[buttonSetting setFrame:CGRectMake(20, 15, 24, 24)];
[buttonSetting addTarget:self action:@selector(settingPage) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *settingbarItem = [[UIBarButtonItem alloc] initWithCustomView:buttonSetting];

- (void) settingPage
{
    [self performSegueWithIdentifier:@"settingPage" sender:nil];
}

修改后的视图控制器:

#import "IconsHelper.h"

UIBarButtonItem *settingbarItem = [IconsHelper setSettingButtonForSelector:@selector(settingPage) target:[ListVC self]];

在IconsHelper.m

+(UIBarButtonItem *)setSettingButtonForSelector:(SEL)selector target:(Class) classObject{
UIImage *buttonImg = [[UIImage imageNamed:@"img-settings"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIButton *buttonSetting = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonSetting setTintColor:[ColorHelper hiltiDirtyPurple]];
[buttonSetting setImage:buttonImg forState:UIControlStateNormal];
[buttonSetting setFrame:CGRectMake(20, 15, 24, 24)];
[buttonSetting addTarget:classObject action:selector forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *settingbarItem = [[UIBarButtonItem alloc] initWithCustomView:buttonSetting];
return settingbarItem;

}

2 个答案:

答案 0 :(得分:2)

请在IconsHelper Class中尝试此更新方法

Updated:

    +(UIBarButtonItem *)setSettingButtonForSelector:(SEL)selector target:(UIViewController *)viewController{
    UIImage *buttonImg = [[UIImage imageNamed:@"img-settings"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    UIButton *buttonSetting = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonSetting setTintColor:[ColorHelper hiltiDirtyPurple]];
    [buttonSetting setImage:buttonImg forState:UIControlStateNormal];
    [buttonSetting setFrame:CGRectMake(20, 15, 24, 24)];
    [buttonSetting addTarget:viewController action:selector forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *settingbarItem = [[UIBarButtonItem alloc] initWithCustomView:buttonSetting];
    return settingbarItem;


    Rather than providing "Class" object, try updating parameter type to "UIViewController".

另外,尝试使用以下方法调用此方法:

 UIBarButtonItem *settingbarItem = [IconsHelper setSettingButtonForSelector:@selector(settingPage) target:self];

让我知道它有效。

答案 1 :(得分:0)

我认为你应该试试这些

['-' | '+' | '~' | '!'] SimpleExpr

问题可能会发生 你写过

- (void)settingPage
{
    [self performSegueWithIdentifier:@"settingPage" sender:self];
}

希望你解决了你的问题。 谢谢 Nirav Zalavadia