-1

私はこれをかなり広範囲に検索しましたが、運が悪かったので、些細なことだと確信しています。

要するに水晶玉型アプリ。私は持っている:

self.predictionArray = [[NSArray alloc] initWithObjects:
                        @"Example 1",    
                        @"example 10000", nil];

そして、「magic8」ボタンをタップすると、次のものが選択されます。

- (IBAction)buttonPressed:(UIButton *)sender {
NSUInteger index = arc4random_uniform(self.predictionArray.count);
self.predictionLabel.text = [self.predictionArray objectAtIndex:index];}

これはすべて完璧に機能し、Web 上にはそれに関する情報がたくさんあります。

私がしたいこと:

これらの配列を 3 つと、ボタンを 3 つ用意します。ボタンは単に対応する NSArray を選択し、「magic8」ボタンを押します。

それはかなり単純なはずです。現在のコードで何が起こっているかは理解していますが、どうすればいいのかわかりません。確かに未経験です。

アドバイスを提供できる人に感謝します。私は本当に誰かの時間を無駄にしたくありません.検索する運がなかったので、知識のある人がそれについて説明するのに1分ほどかかると思いました.

4

2 に答える 2

1

シンプルな (しかし最善ではない) 解決策は、インターフェイス ビルダーの 3 つのボタンにタグ 0、1、2 を与えることです。次に、次のように設定predictionArrayします。

@[   @[ @"button 0 123", @"button 0 456" ],
     @[ @"button 1 123", @"button 1 456" ],
     @[ @"button 2 123", @"button 2 456" ]]

次に、buttonPressedメソッドで、ボタンタグに基づいてサブアレイを選択します。

- (IBAction)buttonPressed:(UIButton *)sender {
    if (sender.tag >= 0 && sender.tag < self.predictionArray.count) {
        NSArray *predictionArray = self.predictionArray[sender.tag];
        NSUInteger index = arc4random_uniform(predictionArray.count);
        self.predictionLabel.text = [predictionArray objectAtIndex:index];
    }
}
于 2013-07-05T14:17:00.127 に答える
0

あなたが本当に3ボタンのことをしたいなら:

- (IBAction) buttonAction:(UIButton *) b
{
switch(b.tag)
{
case 1:
{
self.predictionArray = nil;
self.predictionArray = [[NSArray alloc] initWithObjects:
                    @"Example 1",    
                    @"example 10000", nil];
}
break;

 case 2:
{
self.predictionArray = nil;
self.predictionArray = [[NSArray alloc] initWithObjects:
                    @"Example 2",    
                    @"example 20000", nil];
 }

break;
case 3:
{
self.predictionArray = nil;
self.predictionArray = [[NSArray alloc] initWithObjects:
                    @"Example 3",    
                    @"example 30000", nil];
 }

break; 
default:
break;
 }

- (IBAction)buttonPressed:(UIButton *)sender {
NSUInteger index = arc4random_uniform(self.predictionArray.count);
self.predictionLabel.text = [self.predictionArray objectAtIndex:index];
}
于 2013-07-05T14:16:44.250 に答える