在 UIButton 单击上添加多个 UITextField

Add multiple UITextFields on UIButton click

我有一个非常令人困惑的问题。我有定义为公司详细信息的视图,其中包含 5 UITextFieldsUITextFields 是:地址、城市、邮政编码等。现在,有 UIButton 用于添加一个地址,它添加了相同的视图,但需要填充不同的数据。我对此没有解决方案,这几乎就是我所做的:

 self.officeLocationsLabel = [[UILabel alloc]initWithFrame:CGRectMake(LEFT_MARGIN, self.viewForCompanyProfileImage.frame.origin.y+self.viewForCompanyProfileImage.frame.size.height+SPACE_MARGIN*2, SCREEN_WIDTH-LEFT_MARGIN*2, 40)];
    self.officeLocationsLabel.text = AMLocalizedString(@"officeLocations_text", nil);
    [self.officeLocationsLabel setTextColor:[UIColor blackColor]];
    [self.officeLocationsLabel setFont:[UIFont systemFontOfSize:20]];
    [self.mainScroll addSubview:self.officeLocationsLabel];

    self.streetAddressTextField = [self createTextFieldWithFrame:CGRectMake(LEFT_MARGIN, self.officeLocationsLabel.frame.origin.y+self.officeLocationsLabel.frame.size.height+SPACE_MARGIN, SCREEN_WIDTH-LEFT_MARGIN*2, 40) andPlaceholder:AMLocalizedString(@"streetAddress_text", nil)];
    [self.mainScroll addSubview:self.streetAddressTextField];
    self.streetAddressLine = [self createLineUsingOrigins:self.streetAddressTextField];
    [self.mainScroll addSubview:self.streetAddressLine];

    self.additionalAddresInfoTextField = [self createTextFieldWithFrame:CGRectMake(LEFT_MARGIN, self.streetAddressLine.frame.origin.y+self.streetAddressLine.frame.size.height+SPACE_MARGIN, SCREEN_WIDTH-LEFT_MARGIN*2, 40) andPlaceholder:AMLocalizedString(@"additionalAddress_text", nil)];
    [self.mainScroll addSubview:self.additionalAddresInfoTextField];

    self.additionalAddressLine = [self createLineUsingOrigins:self.additionalAddresInfoTextField];
    [self.mainScroll addSubview:self.additionalAddressLine];

    self.countryTextField = [self createTextFieldWithFrame:CGRectMake(LEFT_MARGIN, self.additionalAddressLine.frame.origin.y+self.additionalAddressLine.frame.size.height+SPACE_MARGIN, SCREEN_WIDTH/3, 40) andPlaceholder:AMLocalizedString(@"countryName_text", nil)];
    [self.mainScroll addSubview:self.countryTextField];

    self.countryLine = [self createLineUsingOrigins:self.countryTextField];
    [self.mainScroll addSubview:self.countryLine];


    self.zipCodeTextField = [self createTextFieldWithFrame:CGRectMake(self.countryTextField.frame.origin.x+self.countryTextField.frame.size.width+LEFT_MARGIN, self.additionalAddressLine.frame.origin.y+self.additionalAddressLine.frame.size.height+SPACE_MARGIN, SCREEN_WIDTH-SCREEN_WIDTH/3-LEFT_MARGIN*3, 40) andPlaceholder:AMLocalizedString(@"zipCode_text", nil)];
    [self.mainScroll addSubview:self.zipCodeTextField];
    self.zipCodeLine = [self createLineUsingOrigins:self.zipCodeTextField];
    [self.mainScroll addSubview:self.zipCodeLine];

    self.addOfficeLocation = [[UIButton alloc]initWithFrame:CGRectMake(LEFT_MARGIN, self.zipCodeLine.frame.origin.y+self.zipCodeLine.frame.size.height+SPACE_MARGIN, SCREEN_WIDTH/2, 40)];

    [self.addOfficeLocation setTitle:AMLocalizedString(@"addOfficeLocation_text", nil) forState:UIControlStateNormal];
    [self.addOfficeLocation setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    self.addOfficeLocation.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.mainScroll addSubview:self.addOfficeLocation];

您的问题可以有多种解决方案:

  1. 如果您处理的是有限的一组地址(例如 2 或 3),那么所有这些字段都会出现在您的视图中(scrollView 具体而言)并通过单击按钮显示这些字段(不要忘记增加 scrollView 的内容大小)。不过这张不整洁
  2. 为一组地址字段创建一个 tableView 部分。每次单击按钮时,都会重新加载 table 新部分。

如果您需要更多信息,请告诉我。