gpt4 book ai didi

ios - 选择器和 PerformSelector

转载 作者:行者123 更新时间:2023-11-29 03:23:06 27 4
gpt4 key购买 nike

我有一个包含多个部分的 UITableView。每个部分包含一组不同的数据:电话号码、地址....

对于这些集合中的每一个,我都有一个模型:PhoneNumber、Address。它们完全不同,但有一些共同点。

在我的 UITableView 中,我有一个包含这些模型/类名的数组:

NSMutableArray *类名;

在我的 UITableViewviewDidLoad 中,我对所有这些部分进行了一些初始化:

//section 1: PhoneNumbers
phoneNumbers = [PhoneNumbers getAllIDs];
if (phoneNumbers && (phoneNumbers.count >0)) {
[classNames addObject:@"PhoneNumber"];
[dataIDs addObject:phoneNumbers];
}

我对所有其他部分/模型再次执行此操作:

 //section 2: Addresses
addresses = [Address getAllIDs];
if (addresses && (addresses.count >0)) {
[classNames addObject:@"Address"];
[dataIDs addObject:addresses];
}
// section 3: .....

到目前为止初始化好了。这看起来不错并且工作正常。

然后在我的 cellForRowAtIndexPath 中,我通过这些 ID 检索实际数据

NSInteger section = [indexPath section];                    
NSInteger row = [indexPath row];

NSArray *rows = [dataIDs objectAtIndex:section];
NSNumber *recordID = [rows objectAtIndex:row];

然后我弄清楚我们必须在哪个类中获取实际数据:

Class displayedDataClass = NSClassFromString ([classNames objectAtIndex:section]);

并获取数据以填充单元格。

id displayedRecord = [[displayedDataClass alloc] init];      
[displayedRecord getByID:recordID];

然后我可以使用以下方法在我的单元格中设置标签:

[cell.someLabel setText:[displayRecord fullDesciption]];

到目前为止一切顺利,我成功地抽象了所有内容,cellForRowAtIndexPath 不需要知道事物的来源,只要这些类响应检索标签数据的方法(在上面的情况下 fullDesciption)

现在我需要在每个 Cell 中执行某种操作的 actionButton为了确保我理解选择器和 performSelection 的概念,我只是在我的 TableView 类中快速而肮脏地进行操作:

- (void) buttonTarget {
NSLog (@"yes");
}

并且在我的 cellForRowAtIndexPath 方法中创建了一个具有以下目标的按钮:

button addTarget:self action:@selector(buttonTarget) forControlEvents:UIControlEventTouchUpInside];

好的,到目前为止一切顺利,一切都按预期进行。但这不是我真正想要的。该操作不应在此处执行,而应在实际类(PhoneNumber、Address、...)中执行。

为了保持整洁,我制作了一个模型 Action,其中包含按钮的图标、描述和选择器:

@interface Action : NSObject

@property (nonatomic, strong) NSString *description;
@property (nonatomic, strong) UIImage *icon;
@property (nonatomic ) SEL selector;

@end

在我的 PhoneNumber 类(和类似类)中,操作被设置为正确的选择器:

Action  *phoneAction = [[Action alloc] init];

phoneAction.description = NSLocalizedString(@"Call", @"Call button description");
phoneAction.icon = [UIImage imageNamed:@"phone"];
phoneAction.selector = @selector(callPhone);

当然callPhone是在PhoneNumber类中实现的。

然后在我的 TableView 中获取该单元格的操作

action = [displayedRecord action];

然后我尝试在我的 Button 中使用该选择器:

[button addTarget:displayedRecord action:[action selector] forControlEvents:UIControlEventTouchUpInside];

但这里出了问题:我们从未到达该方法,我收到以下错误:

[UIDeviceWhiteColor callPhone]: unrecognized selector sent to instance 0x874af90 2013-12-29 23:23:03.629 thinx[27242:907] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIDeviceWhiteColor callPhone]: unrecognized selector sent to instance 0x874af90'

最佳答案

听起来你有一个僵尸。当您将一个 Action 发送到一个没有意义的对象时,这通常意味着您的对象在您可以向其发送消息之前已被释放。

在您的例子中,您要添加“displayedRecord”作为按钮的目标。

为了让它起作用,您需要在按钮对象的生命周期内保持对 displayedRecord 调用的强引用。什么拥有您的 displayedRecord 对象?

如果您不能通过查看您的代码来调试它,您可以使用僵尸工具来尝试解决这个问题。

关于ios - 选择器和 PerformSelector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20830968/

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