从标签发布文本

时间:2011-06-01 06:32:33

标签: iphone

...斐伊川

我正在制作一个iphone应用程序,我使用循环概念以编程方式生成按钮。但无论什么时候我把按钮的值,然后它得到解决我们不能改变按钮的数量。我想更改按钮的数量,我该怎么做。任何人都有解决方案。 请帮帮我。

非常感谢。

代码是:

-(IBAction)button:(id)sender
{
    int z = [txtfrequency.text  intValue];

    NSLog(@"Value of z is :%d",z);
    //int x = 20;
    int y = 250;
    //create a new dynamic button
    for (int j=0; j<z; j++)
    {

        CGRect frame = CGRectMake(20, y , 50, 30);

        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        //[button setTitle:@"Button x" forState:UIControlStateNormal];
        [button setTag:j];

        button.frame = frame;

        CGRect frame1 = CGRectMake(100, y , 50, 30);
        /*UITextField *label = [[UITextField alloc]initWithFrame:frame1];
        //[button setTitle:@"Button x" forState:UIControlStateNormal];
        [label setTag:j];
        label.frame = frame1;*/



        UITextField * textFieldRounded = [[UITextField alloc] initWithFrame:frame1];
        textFieldRounded.borderStyle = UITextBorderStyleNone;
        textFieldRounded.textColor = [UIColor blackColor]; //text color
        textFieldRounded.font = [UIFont systemFontOfSize:17.0];  //font size
        textFieldRounded.backgroundColor = [UIColor whiteColor]; //background color
        textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo;   // no auto correction support

        textFieldRounded.keyboardType = UIKeyboardTypeDefault;  // type of the keyboard
        textFieldRounded.returnKeyType = UIReturnKeyDone;  // type of the return key

        textFieldRounded.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right



        textFieldRounded.delegate = self;
        [textFieldRounded setTag:j];


        [button reloadInputViews];
        [textFieldRounded removeFromSuperview];

        [scroll addSubview:button];
        [scroll addSubview:textFieldRounded];
        y= y+50;


    } 
}

1 个答案:

答案 0 :(得分:0)

在添加新按钮之前,您需要删除现有按钮,否则最终会将它们堆叠起来。最终会耗尽记忆力。您可以指定具有相同标记的容器视图,而不是为按钮和文本字段分配相同的标记。稍后,您可以删除说[[scroll viewWithTag:j] removeFromSuperview];的子视图。

因为它们似乎是行,你也可以通过查看UITableView每行包含一个按钮和文本字段来简化这一过程。对于表视图,更改行数(这是您想要的)将像更改数据源返回的数字一样简单。