0
所以我想要做的是有一個屏幕,每當我按下按鈕它添加一個新的圖像視圖到我創建的UIScrollView,但我遇到了問題...因爲uiscrollview是在viewDidLoad中創建的,所以它沒有被聲明用於後面的方法......所以我的問題基本上是......我如何解決這個問題?以編程方式添加UIScrollView在多種方法中使用
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];
}