gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:55:26 24 4
gpt4 key购买 nike

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

以前的 View Controller :

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

修改后的 View Controller :
#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;

}

最佳答案

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

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

让我知道它正在工作。

关于ios - 在尝试将选择器作为方法的参数调用时将无法识别的选择器发送到类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42945534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com