objective-c - 使用 IBOutletCollections

标签 objective-c ios xcode nsarray iboutletcollection

我有 30 个 UILabel,希望用作 IBOutlet。但是,当我尝试访问它们的 UILabel 属性时,我收到错误消息,提示我找不到类型为“id”的对象的属性 x。我对 Objective C 很生疏,所以怀疑我做了一些根本性的错误。我已将所有标签分配给 xib 文件中的 IBCollection。

.h

@interface ViewController : UIViewController
{
    IBOutletCollection(UILabel) NSArray *statPanels;
}
@property(retain) IBOutletCollection(UILabel) NSArray *statPanels;
@end

.m

@interface ViewController ()
@end

@implementation ViewController
@synthesize statPanels;

- (void)viewDidLoad
{
    [super viewDidLoad];

    statPanels = [[NSArray alloc] initWithObjects:[UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], nil ];

    [statPanels objectAtIndex:3].hidden = YES;
}

最佳答案

如果您在界面构建器中连接了所有标签,那么您不必初始化 statPanels 数组。

删除这一行:

statPanels = [[NSArray alloc] initWithObjects:[UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], [UILabel alloc], nil ];

那条线正在创建一个新数组和一堆新标签,并丢失了你的 socket 。

此外,您需要像其他答案所说的那样转换:

((UILabel *) [statPanels objectAtIndex:3]).property = ....

关于objective-c - 使用 IBOutletCollections,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11976502/

相关文章:

ios - 缺少 Windows 选项的远程 iOS 模拟器?

iphone - 解读苹果崩溃报告

ios - 从 2 个 View Controller 传递数据而不使用 segue

ios - 在动态 tableView 中重新加载包含 textFields 的单元格时出现奇怪的行为

ios - 找不到 Xcode 64BitConversion

ios - 是否无法使用 NSSortDescriptor 按 BOOL 属性排序?

ios - 如何从视频中提取图像?

c++ - Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)

iphone - 有没有办法在 iOS 中为 float 添加逗号,或者至少将其显示为货币?

ios - 根据数组中的项目数量创建 View