按钮不会添加到视图

时间:2012-05-14 15:13:02

标签: iphone objective-c ios xcode for-loop

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

为什么?我做错了什么?

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;
    }
}

非常感谢!

1 个答案:

答案 0 :(得分:1)

我认为当你的viewWillDis出现时,这个视图会被清除,所以请注意......  如果你创建一个方法,你只需粘贴上面的代码,然后当你的ViewController出现时调用你的方法试试这个......

-(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;
        }
    }
}
希望这对你有所帮助.... :)