我有一个令人困惑的问题。我有定义为公司详细信息的视图,其中包含5个UITextFields,而UITextFields分别是:地址,城市,邮政编码等。现在,有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];

最佳答案

您的问题可能有很多解决方案:

  • 如果您处理的是有限的一组地址(例如2或3),则视图上的所有这些字段(具体来说是scrollView)并单击按钮即可显示这些字段(不要忘记增加scrollView的内容大小)。虽然这是,但不是整洁的
  • 创建带有部分地址字段集的tableView。而且,每当您单击按钮时,都会用新部分重新加载表格。

  • 让我知道您是否需要更多信息。

    关于ios - 在UIButton上添加多个UITextFields单击,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33342260/

    10-09 10:21