'attach multiple UITextFields to one iBOutlet

I want to link multiple UITextFields to one iBOutlet. I have tried the suggestion from the following link Multiple IBOutlets in same line of same type in Swift

But this did not work as I got the following error.

'weak' may only be applied to class and class-bound protocol types, not 
'Array<UITextField>'

 @IBOutlet weak var driverTextField: Array<UITextField>=[]

Simulator and code side by side.

Basically, I want to connect the last 3 UITextfields in the simulator to the driverTextfield.



Solution 1:[1]

You have:

@IBOutlet weak var driverTextField: Array<UITextField>=[]

The compiler rightly complains that you can't say weak with an array. But you need an array in order to make an outlet collection. So just delete weak and you'll be fine.

Basically, I want to connect the last 3 UITextfields in the simulator to the driverTextfield

Yup, once you delete weak, your code will compile and you'll be able to configure that in Interface Builder (storyboard).

Solution 2:[2]

you could make an "outlet collection" instead. and access each of the outlets with and for loop and index. @IBOutlet var driverTextField: [UITextField]!

Select outlet collection from dropdown

Select the outlet collection from the dropdown and drag and drop all outlets of textfields to this outlet.

Solution 3:[3]

addEventListener takes a function as its second argument. What you are doing is calling the deleteEvent function and putting the result of the function there instead.

You can create an anonymous function that gets triggered instead and call deleteEvent from within that. You can then pass whatever you want into it.

function appendingEvents() {
  for (let i = 0; i < eventFullInfo.title.length; i++) {
    let classes = document.querySelector(eventFullInfo.class[i]);

    document.querySelector(`.delete-${eventFullInfo.class[i].substring(1)}`).addEventListener("click", () => {
      deleteEvent(classes, i)
    })
  }
}


function deleteEvent(classes, i) {
  classes.innerHTML = "";
  eventFullInfo.class.splice(i, 1);
  eventFullInfo.title.splice(i, 1);
  eventFullInfo.date.splice(i, 1);
  eventFullInfo.type.splice(i, 1);
  eventFullInfo.desc.splice(i, 1);
  eventFullInfo.etime.splice(i, 1);
  eventFullInfo.stime.splice(i, 1);

}

Solution 4:[4]

The event listener callback function has one argument that is passed: the event object. The easiest way to pass custom data is creating an anonymous function which invokes your function.

function appendingEvents() {
  for (let i = 0; i < eventFullInfo.title.length; i++) {
    let classes = document.querySelector(eventFullInfo.class[i]);
    
    document.querySelector(`.delete-${eventFullInfo.class[i].substring(1)}`).addEventListener("click", function(){ deleteEvent(classes, i) });
  }
}

An alternate solution is to use .bind().

function appendingEvents() {
  for (let i = 0; i < eventFullInfo.title.length; i++) {
    let classes = document.querySelector(eventFullInfo.class[i]);
    
    document.querySelector(`.delete-${eventFullInfo.class[i].substring(1)}`).addEventListener("click", deleteEvent.bind(null, classes, i));
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 matt
Solution 2
Solution 3 Andrew Lohr
Solution 4 svrbst