gpt4 book ai didi

ios - -[__NSCFConstantString 大小] : unrecognized selector sent to instance . ..?

转载 作者:可可西里 更新时间:2023-11-01 17:08:36 26 4
gpt4 key购买 nike

redSegmentedControl.m

redSegmentedControl.h

现在,我在另一个 Controller 中编写代码

 // self.segmentedControl = [[redSegmentedControl alloc] initWithFrame:CGRectMake(0, 140, 320, 50)];

self.segmentedControl = [[redSegmentedControl alloc] initWithSectionImages:@[@"list.png", @"list.png", @"list.png"] sectionSelectedImages:@[@"list.png", @"list.png", @"list.png"]];


//[self.segmentedControl setSectionTitles:@[@"List", @"Activity", @"Top Tags"]];

如果删除注释行,它工作正常,但如果运行这段代码,它会出错。

-[__NSCFConstantString size]: unrecognized selector sent to instance ...

我追踪并在 - (void)updateSegmentsRects 方法的这些行中发现了问题。

 for (UIImage *sectionImage in self.sectionImages) {
CGFloat imageWidth = sectionImage.size.width + self.segmentEdgeInset.left + self.segmentEdgeInset.right;
self.segmentWidth = MAX(imageWidth, self.segmentWidth);
}

在这里,它访问了放在数组中的图像的大小,但是出错了,谁能替换代码或告诉我原因。

最佳答案

您的 redSegmentedControl 类期望 sectionImages 是一个 UIImage 对象数组,但是您上面的代码使用 initWithSectionImages 初始化该对象 向它传递一个 NSStrings 数组。因此错误。

所以您可能想要更改 init 方法的调用者:

self.segmentedControl = [[redSegmentedControl alloc]
initWithSectionImages:@[[UIImage imageNamed:@"list.png"], ...]];

或者更改 redSegmentedControl 实现,使其确实期望 sectionImages 是一个 NSString 数组。可能前一个选项会更好,这样调用者就可以负责如何创建图像,控件不必关心这个。

关于ios - -[__NSCFConstantString 大小] : unrecognized selector sent to instance . ..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18289011/

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