0

IBOutletsとして使用したいUILabelが30個あります。ただし、UILabelプロパティにアクセスしようとすると、タイプ'id'のオブジェクトのプロパティxが見つからないというエラーが表示されます。私はObjectiveCに非常に錆びているので、根本的に間違ったことをしたのではないかと思います。すべてのラベルを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;
}
4

3 に答える 3

3

Interface Builderですべてのラベルを接続した場合は、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 ];

その行は、新しい配列と新しいラベルの束を作成し、アウトレットを失っています。

また、他の答えが言っているようにキャストする必要があります:

((UILabel *) [statPanels objectAtIndex:3]).property = ....
于 2012-08-15T20:20:08.743 に答える
1

キャストを使うべきだと思います。NSArrayの束が含まれていることだけを知っていますid。だからあなたは次のようなことをする必要があります

((UILabel *)[array objectAtIndex:0]).someProperty

alloc initまた、だけではなく、持っている必要がありますalloc。また、ivar宣言では、必要IBOutlet...ありません。ただNSArray。(XCodeの比較的新しいバージョンでは、ivarを宣言する必要はまったくありません。)

于 2012-08-15T20:15:15.830 に答える
0

ペン先が逆シリアル化されると、ペン先で指定されたオブジェクトがインスタンス化され、それらのアウトレットに割り当てられます。オブジェクトを自分でインスタンス化する必要はありません。そうすることで、問題のラベルへの唯一の参照が失われます。

基本的に、次の行を削除する必要があります。

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

また、イニシャライザーを呼び出さずにオブジェクトを割り当てると、正しく終了しないことに注意する必要があります。あなたはこれをすることになっていない。Objective-Cの通常のパターンは、電話[[Foo alloc] init]などです。

于 2012-08-15T20:24:56.577 に答える