Outlets Cannot Be Connected to Repeating Content Ios

Outlets cannot be connected to repeating content iOS

Create a table view cell subclass and set it as the class of the prototype. Add the outlets to that class and connect them. Now when you configure the cell you can access the outlets.

Outlets cannot be connected to repeating content

It sounds like you are trying to connect the button to your ViewController, you cannot do this because the cell the button is in repeats. To connect the button you need to connect the button to a UICollectionViewCell class. I'll give you an example below on how to set up your cell.

class Cell: UICollectionViewCell {

@IBOutlet var button: UIButton!

override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

Update per comment:

class ViewController: UIViewController {
//other code
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! Cell
cell.button.addTarget(target: self, action: #selector(buttonTapped), for: .touchUpInside)

return cell
}

@objc func buttonTapped() {
print("do stuff here")
}
}

Swift - Outlets cannot be connected to repeating content

Your table view can have static content or dynamic content.

If you want the table view to have always the same content, set it to static (in interface builder), and then you can link the outlets like that, in the UIViewController.

If you want the table view cells to change dynamically, you cannot do it that way. Because you could repeat cells and the outlet would be ambiguous. You need to create a UITableViewCell subclass for your cells, and create the outlets there.

To clarify: in dynamic table mode, you need to ctrl+drag the outlet into the UITableViewCell subclass, not the view controller.

Outlets cannot be connected to repeating content' in subclass of UICollectionViewCell

I'd suggest you double check the "connections inspector" (the last tab on the panel on the right) for that control (and any other controls inside that cell). It sounds like something in the cell has a lingering outlet hooked up to the view controller. The connections inspector will help you identify that:

Sample Image

Make sure the view controller doesn't show up as one of the outlets. In the above example, I have "accidentally" created two outlets for this label, one to the cell subclass (which is correct) and one to the view controller (which is incorrect).

That will result in a compile-time error that says:

error: The customLabel outlet from the ViewController to the UILabel is invalid. Outlets cannot be connected to repeating content.

If you delete the outlet connection between the cell and the view controller (or whatever non-cell class it's hooked up to), and this compile-time error will go away.

Note, the sentence before the "Outlets cannot be connected to repeating content" message will tell you precisely which outlet is causing the problem. You can even click on this error inside the "issues navigator" in the left panel and Xcode will jump to the storyboard and select the offending control (at which point you can directly open up the connections inspector) and find the offending outlet.

Xcode Error: Outlets cannot be connected to repeating content

This message only occurs if you connect it to the view controller. As I have already commented, you probably did not delete the first connection outlet you've made to your view controller. Even if you delete the IBOutlet code from your view controller you still need to right click it and delete the old connection that probably still there. After deleting it the error message will go away.

Outlets cannot be connected to repeating content iOS 5

Your answer is that you use an object in UITableViewCell by reusing it,

So you can't create it's outlet except you create Your class for your "UITableViewCell".

Below is a reference for you,

I hope that it's useful for you.

You need to drag and drop the object in your UITableViewCell,
Then you have to give tag for that object.

then you can use it with its tag.

First give an identifier to UITableViewCell,

Below is a reference image for it.

Sample Image

Then give a tag to your UI object,
As this in this reference image.

Sample Image

Below is sample code for you which I use regularly,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


UITableViewCell *Cell = [self.TableListRegion dequeueReusableCellWithIdentifier:@"List"];

UIButton *objectOfButton = (UIButton *)[CellLast viewWithTag:200];

[objectOfButton addTarget:self action:@selector(YourSelector:) forControlEvents:UIControlEventTouchUpInside];

return Cell;

}

Now you can receive that button event by,

-(IBACTION)YourSelector:(id)sender{
// Your Button in Cell is selected.
// Do your stuff.

}

Feel free to ask if you need more help regarding it.

Outlets cannot be connected to repeating content in CollectionView Cells

You need to subclass UICollectionViewCell which you should use in your collection view. The subclass is where you will add the outlet.

Accessing IBOutlet from subclass, outlets cannot be connected to repeating content subclass

You set the button properties (such as Title) in cellForItemAt.

Side note: using Subclass as the name of your class will be very confusing.

Here's a quick example:

class MyButtonCell: UICollectionViewCell{
@IBOutlet weak var buttonOne: UIButton!

var callback: (() -> ())?

@IBAction func buttonTapped(_ sender: UIButton) {
callback?()
}
}

class TestCollectionViewController: UICollectionViewController {

let buttonTitles: [String] = [
"First", "Second", "Third", "etc..."
]

override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return buttonTitles.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCellID", for: indexPath) as! MyButtonCell
cell.buttonOne.setTitle(buttonTitles[indexPath.item], for: [])
cell.callback = {
print("Button was tapped at \(indexPath)")
// do what you want when the button is tapped
}
return cell
}
}

Notice that I also added an @IBAction for the button inside the cell subclass. I also added this var / property:

var callback: (() -> ())?

That makes it easy to set up a closure in your controller code - again, in cellForItemAt - allowing your controller to handle and act when the button in a cell is tapped.


Edit

Here is a complete implementation:

class MyButtonCell: UICollectionViewCell{
@IBOutlet weak var buttonOne: UIButton!

var callback: ((UICollectionViewCell) -> ())?

override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() -> Void {
contentView.layer.borderWidth = 1
contentView.layer.borderColor = UIColor.black.cgColor
}

@IBAction func buttonTapped(_ sender: UIButton) {
callback?(self)
}
}

class StevenViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

let buttonTitles: [String] = [
"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"
]

@IBOutlet var collectionView: UICollectionView!

override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return buttonTitles.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCellID", for: indexPath) as! MyButtonCell

// set the button title (and any other properties)
cell.buttonOne.setTitle(buttonTitles[indexPath.item], for: [])

// set the cell's callback closure
cell.callback = { [weak self] theCell in
guard let self = self,
let indexPath = collectionView.indexPath(for: theCell)
else { return }
print("Button was tapped at \(indexPath)")
// do what you want when the button is tapped
}

return cell
}
}

And here's the Storyboard source - if you haven't done this before, create a new Storyboard, select Open As -> Source Code, delete what's there, copy and paste the following... then you can select Open As -> Interface Builder -Storyboard:

The result when you run it should look like this (I gave the collection view a yellow background to make it easy to see the frame):

Sample Image



Related Topics



Leave a reply



Submit