Interface Builder, @Iboutlet and Protocols for Delegate and Datasource in Swift

Interface Builder, @IBOutlet and protocols for delegate and dataSource in Swift

From the Xcode release notes:

Interface Builder does not support connecting to an outlet in a Swift file when the outlet’s type is a protocol.

Workaround: Declare the outlet's type as AnyObject or NSObject, connect objects to the outlet using Interface Builder, then change the outlet's type back to the protocol.

EDIT: Xcode 9 beta 3 release notes say that this workaround should no longer be necessary.

Is it possible to connect delegate and dataSource of CustomViews in interface builder?

You'll need to meet these conditions:

  1. The view's Custom Class should be set to your custom view's class name in Interface Builder (via the Identity Inspector). If your delegate or dataSource object is also a custom view, also make sure that that view's Custom Class is set
  2. The @interface for your custom class should decorate its delegate and dataSource properties with IBOutlet. For example, @property (nonatomic, weak) IBOutlet id <SomeProtocol> delegate;
  3. If you declared protocol(s) for your delegate or dataSource, the target object that you want to use as the delegate or dataSource should be declared as implementing that protocol

How to link Table dataSource and delegate outlets to UITableViewController?

In your storyboard, select the UITableView and change this to the name of your UITableViewController subclass. You then do not need to do any linking stuff.

Sample Image

Can not connect custom protocol delegate from storyboard in Xcode 6.1

https://developer.apple.com/library/ios/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html#//apple_ref/doc/uid/TP40001051

Interface Builder

Interface Builder does not support connecting to an outlet in a Swift
file when the outlet’s type is a protocol. Declare the outlet's type
as AnyObject or NSObject, connect objects to the outlet using
Interface Builder, then change the outlet's type back to the protocol.
(17023935)

it's sucks...

swift protocol, IBOutlet property cannot have non-object type

You have to declare your ThumbnailTableViewCellDelegate protocol as @objc:

@objc protocol ThumbnailTableViewCellDelegate {
func cellWasTouched(thumbnail: Bool, cell: UITableViewCell)
}

This is because @IBOutlet declares the variable as weak, which only works with objects. I'm not sure why you can't just say the protocol conforms to AnyObject, perhaps that's a Swift bug.

@IBInspectable in Swift 4.0

Add notation @objc for both delegate and datasource in Swift 4 (as shown in below code)

open class SVContactBubbleView: UIView {
@IBInspectable open var dataSource: SVContactBubbleDataSource?
@IBInspectable open var delegate: SVContactBubbleDelegate?
}

@objc // add notation here
public protocol SVContactBubbleDataSource
{
//Methods here
}


@objc // add notation here
public protocol SVContactBubbleDelegate
{
//Methods here
}



Here is ref. snapshot with error resolution:

Sample Image

Here is Apple document for @objc notation - Protocol - Optional Protocol Requirements

@IBOutlet implemented as property wrapper?

No. @IBOutlet is a "Declaration Attribute", documented here.

Reference main NSWindow in AppDelegate using Storyboard?

I dont know if this is correct way of doing, but this will solve your problem.

Decalre a NSWindow property in AppDelegate

weak var window: NSWindow!

and set the property in something like windowWillLoad of the NSWindowController

(NSApplication.sharedApplication().delegate as! AppDelegate).window = self.window

You will have to subclass NSWindowController to define windowWillLoad



Related Topics



Leave a reply



Submit