以编程方式添加了多种方法中使用的UIScrollView

时间:2012-05-10 19:32:34

标签: uiscrollview

所以我要做的就是有一个屏幕,每当我按下一个按钮,它就会为我创建的UIScrollView添加一个新的图像视图,但我遇到了一个问题...因为uiscrollview是在viewDidLoad它没有声明在后面的方法中使用...所以我的问题基本上就是......我该如何解决这个问题?

int pixels = 20;
int playerCount = 2;
-(void) viewDidLoad{

UIScrollView *mainScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
mainScroll.contentSize = CGSizeMake(320, 700);
mainScroll.showsHorizontalScrollIndicator = YES;
[self.view addSubview:mainScroll];
[mainScroll setScrollEnabled:YES];


UIButton *addPlayersButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];

[addPlayersButton addTarget:self
                     action:@selector(addPlayer)
           forControlEvents:UIControlEventTouchUpInside];
[mainScroll addSubview: addPlayersButton];
    addPlayersButton.frame = CGRectMake(20, pixels, 50, 50);



-(void)addPlayer{
pixels += 90;
playerCount++;

if (playerCount <= 7) {
    UIImageView *player3 = [[UIImageView alloc]initWithFrame: CGRectMake(15, pixels, 290, 80)];
    player3.image = [UIImage imageNamed:@"player1Bar.png"];
    [mainScroll addSubview:player3];
}

1 个答案:

答案 0 :(得分:0)

我发现我需要做的是在.h文件中创建UIScrollView并合成它以便能够在多种方法中使用它