0

スクロールビューに追加する44個のボタンがあり、一連の配列を使用してそうしようとしていました。ファイル名の名前文字列、写真の Y サイズ (すべての x は同じ) などの配列があります。

メソッドを実行するviewDidLoadにあるコードは次のとおりです。

for (int i = 0; i < 44; i++) {
    [self makeLabelsAndButtons:i];
}

メソッド makeLabelsAndButtons は次のとおりです。

-(void)makeLabelsAndButtons:(NSInteger *)indexPath{
NSString *nameString = [nameArray objectAtIndex:indexPath];
NSString *picString = [picArray objectAtIndex:indexPath];
NSInteger picNumb = [[numbers objectAtIndex:indexPath] integerValue];
NSInteger picXnum = [[picXCoords objectAtIndex:indexPath] integerValue];

Button = [UIButton buttonWithType:UIButtonTypeCustom];
[Button addTarget:self action:@selector(presPressed:)
  forControlEvents:UIControlEventTouchUpInside];
Button.frame = CGRectMake(160.0, 240.0, 220.0, picNumb);
Button.center = CGPointMake(picXnum, 200.0);
[Button setBackgroundImage:[UIImage imageNamed:picString] forState: UIControlStateNormal];
[ScrollView addSubview:Button];
[ScrollView insertSubview:Button atIndex:1];

NSLog(@"name: %@, pic:%@, picY:%i", nameString, picString, picNumb);

}

ボタンは追加されていませんが、エラーは発生しません。何が間違っているのか本当にわかりません

助けてくれてありがとう!

編集:これは、スクロールビューを初期化する方法です

ScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)];
ScrollView.showsVerticalScrollIndicator=YES;
ScrollView.scrollEnabled=YES;
ScrollView.bounces = YES;
ScrollView.userInteractionEnabled=YES;
[self.view addSubview:ScrollView];
[self.view insertSubview:ScrollView atIndex:1];
ScrollView.contentSize = CGSizeMake(20000,480);

NSLogs のシステムを介して、ボタンがビューに追加されていないと判断しましたが、その理由はわかりません。

4

3 に答える 3

0

あなたの scrollView は水平方向にスクロールできるようです。

あなたが試すことができます

-(void)addButtonsToScrollView
{
    //X Offset for button seperation 
    CGFloat xOffset = 10.0f;

    NSUInteger buttonCount = 0;
    //In each iteration buttonFrame would be adjusted to next buttonFrame
    //This would be used to find the contentSize of scrollView
    CGRect buttonFrame = CGRectMake(xOffset, 240.0, 220.0, 40.0f);
    while (buttonCount<44)
    {
        NSString *nameString = nameArray[buttonCount];
        NSString *picString = picArray[buttonCount];

        CGFloat picNumb = [numbers[buttonCount] floatValue];
        CGFloat picXnum = [picXCoords[buttonCount] floatValue];

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        [button addTarget:self
                   action:@selector(presPressed:)
         forControlEvents:UIControlEventTouchUpInside];

        buttonFrame.origin.y = (scrollView.frame.size.height - picNumb)/2;
        buttonFrame.size.height = picNumb;
        button.frame = buttonFrame;

        [button setBackgroundImage:[UIImage imageNamed:picString]
                          forState: UIControlStateNormal];
        [button setTitle:nameString
                forState:UIControlStateNormal];

        [scrollView addSubview:button];

        buttonFrame.origin.x += buttonFrame.size.width+xOffset;
        buttonCount++;

    }

    CGSize contentSize = scrollView.frame.size;
    contentSize.width = buttonFrame.origin.x;
    scrollView.contentSize = contentSize;

}
于 2013-04-20T20:50:07.837 に答える
0

次のようにコードを変更してください.....うまくいくかもしれません

[Button setBackgroundImage:[UIImage imageNamed:picString] forState: UIControlStateNormal];

上記を次のように置き換えます

NSString *path = [[NSBundle mainBundle] pathForResource:@"picString" ofType:@"png"];
[Button setBackgroundImage:[[UIImage alloc] initWithContentsOfFile:path] forState: UIControlStateNormal];
于 2013-04-20T20:22:41.877 に答える
0

Button2 つのプロパティがありScrollView、コントローラーで宣言されていると仮定します。

  • ScrollView読みやすくするために、プロパティの名前を に変更しますscrollView

    @property (nonatomic, weak) IBOutlet UIScrollView *scrollView;
    
  • Buttonプロパティは不要なので削除します。

  • コードを更新します。

    Button *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(presPressed:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(160.0, 240.0, 220.0, picNumb);
    button.center = CGPointMake(picXnum, 200.0);
    [button setBackgroundImage:[UIImage imageNamed:picString] forState: UIControlStateNormal];
    [self.scrollView addSubview:button];
    

サブビューの追加と挿入の両方を行う必要はありません。

ScrollViewnil オブジェクトにメッセージを割り当てて送信しても、エラーは発生しない可能性があります。self.scrollView が nil でないことを確認する必要があります。nil の場合は、ビューで適切に接続されていることを確認してください。

于 2013-04-20T20:36:29.187 に答える