【问题标题】:Programmatically added UIScrollView used in multiple methods以编程方式添加在多个方法中使用的 UIScrollView
【发布时间】:2012-05-10 19:32:33
【问题描述】:

所以我想要做的是有一个屏幕,每次我按下按钮时,它都会向我创建的 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];
}

【问题讨论】:

    标签: uiscrollview


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-29
      • 2013-04-08
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 1970-01-01
      • 2017-06-28
      相关资源
      最近更新 更多