0

我想创建一个有 4 个按钮的行,循环正在做它应该做的事情,并且它只输入了 4 次 if 语句,但是当视图弹出时,我只能看到一个按钮。

这是为什么?难道我做错了什么?

btnFrame = 18;
for (int i = 0; i < [arrImages count]; i++)
{
    if (btnFrame <= 237)
    {
        NSLog(@"%i",btnFrame);
        UIButton * tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        tempBtn.frame = CGRectMake(btnFrame, 20, 65, 65);
        [tempBtn setTitle:[NSString stringWithFormat:@"Button%i",i] forState:UIControlStateNormal];
        [self.view addSubview:tempBtn];
        btnFrame = btnFrame + 73;
    }
}

非常感谢!

4

1 回答 1

1

我认为当你的 viewWillDisappear 那时这个视图将被清除,所以做下面的事情......

-(void)viewWillAppear:(BOOL)animated
{
   [self SetButton];    
}

-(void)setButton{

btnFrame = 18;
    for (int i = 0; i < [arrImages count]; i++)
    {
        if (btnFrame <= 237)
        {
            NSLog(@"%i",btnFrame);
            UIButton * tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            tempBtn.frame = CGRectMake(btnFrame, 20, 65, 65);
            [tempBtn setTitle:[NSString stringWithFormat:@"Button%i",i] forState:UIControlStateNormal];
            [self.view addSubview:tempBtn];
            btnFrame = btnFrame + 73;
        }
    }
}

希望这对你有帮助.... :)

于 2012-05-14T15:25:42.600 回答