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