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")
}
}
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:
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.
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.
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.
Then give a tag to your UI object,
As this in this reference 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
:
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16096" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="81J-PL-nYH">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="collection view cell content view" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Steven View Controller-->
<scene sceneID="UOc-kB-D4u">
<objects>
<viewController id="81J-PL-nYH" customClass="StevenViewController" customModule="FirstNewMini" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="5pE-V8-AOY">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="prototypes" translatesAutoresizingMaskIntoConstraints="NO" id="eKk-rE-Fyn">
<rect key="frame" x="20" y="567" width="335" height="60"/>
<color key="backgroundColor" red="0.99942404029999998" green="0.98555368190000003" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="60" id="t8R-1V-ELi"/>
</constraints>
<collectionViewFlowLayout key="collectionViewLayout" scrollDirection="horizontal" automaticEstimatedItemSize="YES" minimumLineSpacing="10" minimumInteritemSpacing="10" id="34u-td-lg8">
<size key="itemSize" width="60" height="60"/>
<size key="headerReferenceSize" width="0.0" height="0.0"/>
<size key="footerReferenceSize" width="0.0" height="0.0"/>
<inset key="sectionInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
</collectionViewFlowLayout>
<cells>
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="myCellID" id="slK-WV-d6z" customClass="MyButtonCell" customModule="FirstNewMini" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="60" height="60"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<collectionViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" insetsLayoutMarginsFromSafeArea="NO" id="inr-uV-Mkf">
<rect key="frame" x="0.0" y="0.0" width="60" height="60"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="jO2-43-Y97">
<rect key="frame" x="7" y="15" width="46" height="30"/>
<color key="backgroundColor" red="0.92143100499999997" green="0.92145264149999995" blue="0.92144101860000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<state key="normal" title="Button"/>
<connections>
<action selector="buttonTapped:" destination="slK-WV-d6z" eventType="touchUpInside" id="ILI-g9-h3v"/>
</connections>
</button>
</subviews>
<constraints>
<constraint firstItem="jO2-43-Y97" firstAttribute="centerY" secondItem="inr-uV-Mkf" secondAttribute="centerY" id="K8s-Jl-sfY"/>
<constraint firstItem="jO2-43-Y97" firstAttribute="centerX" secondItem="inr-uV-Mkf" secondAttribute="centerX" id="kF4-R0-H31"/>
</constraints>
</collectionViewCellContentView>
<connections>
<outlet property="buttonOne" destination="jO2-43-Y97" id="EkG-Dz-2DC"/>
</connections>
</collectionViewCell>
</cells>
</collectionView>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<constraints>
<constraint firstItem="OxZ-Gp-ekf" firstAttribute="trailing" secondItem="eKk-rE-Fyn" secondAttribute="trailing" constant="20" id="Gua-Zi-IFQ"/>
<constraint firstItem="OxZ-Gp-ekf" firstAttribute="bottom" secondItem="eKk-rE-Fyn" secondAttribute="bottom" constant="40" id="I6q-K5-nN3"/>
<constraint firstItem="eKk-rE-Fyn" firstAttribute="leading" secondItem="OxZ-Gp-ekf" secondAttribute="leading" constant="20" id="sCP-Nn-RqH"/>
</constraints>
<viewLayoutGuide key="safeArea" id="OxZ-Gp-ekf"/>
</view>
<connections>
<outlet property="collectionView" destination="eKk-rE-Fyn" id="rxw-DZ-Kpi"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Ap0-Qs-M9q" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="199.19999999999999" y="155.17241379310346"/>
</scene>
</scenes>
</document>
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):
Related Topics
Swift Dictionary Initialization of Custom Type Gives: '>' Is Not a Postfix Unary Operator Error
Set Maximum Characters (To One) in a Nstextfield in Swift
How to Access Camera with Swiping Gesture
What Happens While Wrapping and Unwrapping an Optional in Swift
Swift Calendar Class Returning Wrong Date for Weekday
Uinavigationbar Turns White and Won't Close When Using Navigationitem.Searchcontroller in iOS 13
Bold Words That Start with Letter
Spritekit Skscene Not Resizing Correctly to Fit iPhone 12
Convert Gregorian Date to Hijri Date
Create Tab Bar Controller and Navigation Controller
Data Structure for Fast Lookup with Multiple Criteria
Add Value to Variable Inside Closure in Swift
Find Index of Annotation Mapkit
Nsdocumentcontroller.Opendocument Not Allowing Selection of Custom File Type