Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 动态添加uitextfields_Ios_Uitextfield - Fatal编程技术网

Ios 动态添加uitextfields

Ios 动态添加uitextfields,ios,uitextfield,Ios,Uitextfield,我有一个UIActionSheet,上面有4个按钮。我已经有一个文本字段了。如果我按操作表上的2按钮,我必须再添加1个文本字段和第3个按钮,我必须添加2个UITextFields,以此类推,最多添加5个按钮。如果我按第5个按钮,5个文本字段应该被添加,如果我按第1个按钮,剩下的4个文本字段应该被添加,我也必须设置scrollview的contentSize。我写过这样的话: -(void)actionSheet:(UIActionSheet *)actionSheet clickedButton

我有一个
UIActionSheet
,上面有4个按钮。我已经有一个文本字段了。如果我按操作表上的2按钮,我必须再添加1个文本字段和第3个按钮,我必须添加2个
UITextField
s,以此类推,最多添加5个按钮。如果我按第5个按钮,5个文本字段应该被添加,如果我按第1个按钮,剩下的4个文本字段应该被添加,我也必须设置scrollview的contentSize。我写过这样的话:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex


    if(buttonIndex>1)
    {
        float yAxis = _adult1TF.frame.origin.y+_adult1TF.frame.size.height+10;
        float xAxis1 = _adult1TF.frame.origin.x;
        float xAxis2 = _child1TF.frame.origin.x;
        float width = _child1TF.frame.size.width;
        for(int i=1;i<(buttonIndex+1);i++)
        {
            UILabel * roomLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(xAxis1, yAxis, width, 20)];
            roomLabel2.text = [NSString stringWithFormat:@"Room%d:",i+1];
            [_scroll addSubview:roomLabel2];
            UILabel * adultLabel = [[UILabel alloc]initWithFrame:CGRectMake(xAxis1, roomLabel2.frame.size.height+roomLabel2.frame.origin.y+10, width, 20)];
            adultLabel.text = @"Adults";
            [_scroll addSubview:adultLabel];
            UILabel * childLabel = [[UILabel alloc]initWithFrame:CGRectMake(xAxis2, adultLabel.frame.origin.y, width, 20)];
            childLabel.text = @"Children";
            [_scroll addSubview:childLabel];

            UITextField * adultTF = [[UITextField alloc]initWithFrame:CGRectMake(xAxis1, adultLabel.frame.size.height+adultLabel.frame.origin.y+10, width, 30)];
            adultTF.text = @"1";
            [_scroll addSubview:adultTF];

            UITextField * childTF = [[UITextField alloc]initWithFrame:CGRectMake(xAxis2, adultTF.frame.origin.y, width, 30)];
            childTF.text = @"0";
            [_scroll addSubview:childTF];
            adultTF.borderStyle = childTF.borderStyle = UITextBorderStyleRoundedRect;
            yAxis = adultTF.frame.size.height+adultTF.frame.origin.y+10;
        }
        _searchButton.frame = CGRectMake(_searchButton.frame.origin.x, yAxis+10, _searchButton.frame.size.width, _searchButton.frame.size.height);
        _scroll.contentSize = CGSizeMake(_scroll.frame.size.width, _searchButton.frame.size.height+_searchButton.frame.origin.y+15);
    }
-(作废)操作表:(UIActionSheet*)操作表单击按钮索引:(NSInteger)按钮索引
如果(按钮索引>1)
{
float yAxis=_adult1TF.frame.origin.y+_adult1TF.frame.size.height+10;
float xAxis1=_adult1TF.frame.origin.x;
float xAxis2=_child1TF.frame.origin.x;
浮动宽度=_child1TF.frame.size.width;

对于(int i=1;i您已经提到操作表限制了按钮的数量。因此,您可以初始创建所有文本字段并隐藏这些文本字段。当用户单击操作表按钮时,您只需根据按钮索引隐藏/取消隐藏文本字段


如果有更多按钮(要创建更多文本字段)您可以使用UITableView类创建具有resuse功能的多个对象。

在添加文本字段之前,请删除ScrollView中的所有子视图使用tableview并向每个单元格添加文本字段,然后单击检查操作表中是否有按钮被按下,并将行数设置为按钮单击索引。如果要隐藏tableview分隔符。