在UIButton上添加多个UITextField,然后点击

时间:2015-10-26 09:24:33

标签: ios objective-c uibutton uitextfield

我有一个非常混乱的问题。我的视图定义为公司详细信息,其中包含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 个答案:

答案 0 :(得分:2)

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

  1. 如果您正在处理有限的地址集(例如2或3),那么您视图中的所有字段(scrollView都是特定的)将显示那些按钮点击的字段(不要忘记增加scrollView)的内容大小。这个不整洁虽然。
  2. 创建一个tableView,其中包含一组地址字段。每次单击该按钮都会使用新部分重新加载表格。
  3. 如果您需要更多信息,请与我们联系。