-5

これはStackOverflowに関する私の最初の質問なので、十分に明確になっていることを願っています。

iPhone用のゲームをデザインしていますが、一部のオブジェクトの表示に問題があります。オブジェクトのNSMutableArray(UIImageViewをサブクラス化するカスタムオブジェクトクラス)があり、それらすべてを画面に表示できるようにしたいと考えています。ただし、配列はサイズを変更できるため、InterfaceBuilderで各要素を直接リンクすることはできません。誰かが私がこの問題を解決し始める方法について何か考えがありますか?

ありがとう!

コード:

//This code is in the viewController.m file
//The main array of objects is nodeList. Each node subclasses UIImageView.
//nodeList was also synthesized at the beginning of the file with @synthesize.


//initialize array
nodeList= [[NSMutableArray alloc] initWithCapacity:(vertLinks*horizLinks)];

//make center node
Node *n = [Node alloc];

//Initialize the node (just puts an x and y position in, and gives it a mass)
[n nodeInit:(startX):(startY):(nodeMass)];
[nodeList addObject:(n)];

//creates a 'web' of nodes, and adds each one to the array
for (int i = 1; i < vertLinks; i++)
{
    for (int j = 0; j < horizLinks; j++)
    {
        //draw the line to each point
        int nodeX = (startX + cos(((360 / horizLinks) * j) * (M_PI / 180)) * rad * i);
        int nodeY = (startY + sin(((360 / horizLinks) * j) * (M_PI / 180)) * rad * i);

        //add it to the screen
        Node *nd = [Node alloc];

        [nd nodeInit:(nodeX):(nodeY):(nodeMass)];

        if (i == vertLinks - 1)
        {
            nd.solid = true;
        }

        [nodeList addObject:(nd)];
    }
}
4

1 に答える 1

0

正確に答えるのは難しいですが、基本的には、配列内のオブジェクトをループして、それぞれを個別に追加する必要があります。これらのオブジェクトが UIView、UIImageView、UIButton などのビューである場合は、次のようにするだけです。

for (int i=0; i<array.count; i++) {
   UIView *thisView = [array objectAtIndex:i];
   [self.view addSubview:thisView]
}

文字列や数値などの場合は、オブジェクトを表示するためにラベルやテキスト フィールドなどを作成する必要があります。

for (int i=0; i<array.count; i++) {
   NSString *string = [array objectAtIndex:i];
   UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100*i, 100, 100)];
   [label setTitle:string];
   [self.view addSubview:label]
}

あなたが話しているオブジェクトの種類は本当に重要です。本当の助けが必要な場合は、これらがどのような種類の画像であるかをお知らせください。また、適切な投稿方法でもあります。

詳細を投稿してください。喜んで回答を続けます。

于 2012-06-30T07:32:50.320 に答える