简体   繁体   中英

How to get text from dynamically created UITextField without touching or tapping the text field (disabled user interaction)?

In my application I have created textfield dynamically in for loop based on server response, I designed Every row it have five textfields. Here I have multiply the textfield1 value and textfield2 value then display in textfield4 and multiply textfield1 and textfield3 then display in textfield5 so here I have disable user interaction for textfield4 and textfield5 based on placeholder text because I want to just display the values to that textfields and make non-editable fields.

Here I have attached the empty fields screenshot for this functionality:

在此处输入图片说明

Here I have attached the screenshot after filled some values:

在此处输入图片说明

Here my problem is unable to get the textfield4 and textfield5 values because of disable user interaction. If I enable user Interaction means I am able to get the both textfield values in textFieldShouldEndEditing .

How to get the values of textfield4 and textfield5?

You can set the tag for textfield and get the textfield using that tag. Refer the below example code.

if let txtField = self.view.viewWithTag(123) as? UITextField {
    print("txtField.text")
}

To me the simplest solution is to have a var which you set with the calculated textfields value at the same time you set textfield4 and 5 text. If you are looking to get a notification you can add an observer for its value.

A nicer way of doing this might be to lay everything out within a UITableView which is populated by a set of objects, we'll name this object Item . Each Item could have properties such as name , quantity , squareFootPerUnit , estCost , totalSquareFoot and totalCost .

I would then create a UITableViewCell with three UITextFields ( quantity , estCost , squareFootPerUnit ) which can be edited, and three labels ( name , totalSquareFoot and totalCost ). Make your UITableViewCell conform to UITextFieldDelegate to capture the inputs from the user.

In tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) , populate the UITableViewCells with the data from your Item objects. Once you have captured the quantity , estCost , squareFootPerUnit data from the UITextFields , do the calculations and assign the answers to the totalSquareFoot and totalCost properties in your Item object and reload the table.

There are multiple ways of assigning thee calculated values to your Item objects. The way I usually use is a closure(Swift) / block(Objective-C) as a property on the UITableViewCell which I then invoke whenever the calculation has completed. Pass the calculated answers as parameters.

UITableView Advantages

  1. No need to handle any complicated x & y co-ordinate positioning you may have if the server returns you multiple Item data.
  2. Easier to maintain your Item data by using objects.
  3. No need to use tag objects on views or keep ahold of tag names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM