I have a (swift) class which has a weak pointer to a delegate like this:
import UIKit
#objc public protocol DRSlidingPanelViewControllerDelegate : class {
optional func didSlidePanel(panelHidden : Bool , sender : DRSlidingPanelViewController) -> Void
}
public class DRSlidingPanelViewController: UIViewController {
public weak var delegate : DRSlidingPanelViewControllerDelegate?
///other stuff...
}
Now i make a subclass with another protocol which extends the first, and i want to alter the inherited 'delegate' property
#objc public protocol DRTableViewControllerDelegate : DRSlidingPanelViewControllerDelegate {
optional func someFunction(sender : DRTableViewController) -> Void
}
public class DRTableViewController: DRSlidingPanelViewController {
// public weak var delegate : DRTableViewControllerDelegate?
}
^ this (re)declaration of delegate in the subclass gives me 3 errors when I uncomment it.
Property 'delegate' with type 'DRTableViewControllerDelegate?' (aka 'Optional') cannot override a property with type 'DRSlidingPanelViewControllerDelegate?' (aka 'Optional')
Getter for 'delegate' with Objective-C selector 'delegate' conflicts with getter for 'delegate' from superclass 'DRSlidingPanelViewController' with the same Objective-C selector
Setter for 'delegate' with Objective-C selector 'setDelegate:' conflicts with setter for 'delegate' from superclass 'DRSlidingPanelViewController' with the same Objective-C selector
Now i understand the nature of these errors, and that they are different facets of the one error (attempting to change the 'type' on the delegate pointer.) Can anybody give me a clue how to do this? It obviously can be done, look at how UITableView alters the delegate pointer which it inherits from UIScrollView. In objC I would get a warning which could be silenced with an #dynamic.
Thanks and best regards
edit / addition
Thanks Matt, I do see this previous question, but unfortunately it is closed and I would personally not accept that answer as the definitive answer because it is a compromise.
If I right click on UITableView in xCode and 'jump to definition' I see this
#available(iOS 2.0, *)
public class UITableView : UIScrollView, NSCoding {
public init(frame: CGRect, style: UITableViewStyle) // must specify style at creation. -initWithFrame: calls this with UITableViewStylePlain
public init?(coder aDecoder: NSCoder)
public var style: UITableViewStyle { get }
weak public var dataSource: UITableViewDataSource?
**weak public var delegate: UITableViewDelegate?**
//plenty other stuff..
}
So I respectfully submit that there definitely is a way to do this.
I would say that as things stand you can't do it. This is disappointing to say the least. You'll just have to call the delegate variable in the subclass something else.
So, this is legal, but of course it totally fails to meet your requirements:
#objc protocol P1 {
}
#objc protocol P2 : P1 {
}
public class VC1: UIViewController {
weak var delegate : P1?
}
public class VC2: VC1 {
weak var delegate2 : P2?
}
If you really hate the multiplication of storage you can make the delegate2 a computed variable that accesses the inherited delegate:
#objc protocol P1 {
}
#objc protocol P2 : P1 {
}
public class VC1: UIViewController {
weak var delegate : P1?
}
public class VC2: VC1 {
weak var delegate2 : P2? {
set {
super.delegate = newValue
}
get {
return super.delegate as? P2
}
}
}
Related
I wrote simplified version of similar implementation I'm having problem with. Anyone knows why is that and eventually how to workaround?
NOTE: the code is just example to keep it as simple as possible
protocol Alertable {
associatedtype Alert
func show(alertOfType alertType: Alert)
}
protocol ViewControllerDelegate: class, Alertable {
}
final class MyViewController: UIViewController {
// MARK: - Types
enum AlertType {
case alert
case warning
}
}
extension MyViewController: ViewControllerDelegate {
typealias Alert = AlertType // ! here i specify the associated type !
func show(alertOfType alertType: Alert) {
// code..
}
}
So far so good. But, here I get errors:
final class ViewModel {
// ERROR: Protocol 'ViewControllerDelegate' can only be used as a generic constraint because it has Self or associated type requirements.
weak var viewController: ViewControllerDelegate?
init(viewController: ViewControllerDelegate?) {
self.viewController = viewController
}
private func someFunction() {
// ERROR: Member 'show' cannot be used on value of protocol type 'NewsFeedViewControllerInput'; use a generic constraint instead.
viewController?.show(alertOfType: .warning)
// code..
}
}
Thank you
You had a bit of a misunderstanding here. When you define:
protocol ViewControllerDelegate: class, Alertable {}
extension MyViewController: ViewControllerDelegate {
typealias Alert = AlertType // ! here i specify the associated type !
func show(alertOfType alertType: Alert) {
// code..
}
}
The typealias is defined in MyViewController but not ViewControllerDelegate. It's not clear why you need ViewControllerDelegate in this question but maybe there's something we don't see in the real app.
In ViewModel, change from ViewControllerDelegate to MyViewController:
final class ViewModel {
weak var viewController: MyViewController?
// ...
}
One more thing, though unrelated to the error: you use many final classes. Should they be structs instead?
// QuizPopUpViewController.swift
#objc protocol QuizPopUpViewControllerDelegate {
func ApplyNowToSendBack()
}
class QuizPopUpViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextViewDelegate {
weak var delegate: QuizPopUpViewControllerDelegate?
}
// giving event from here
if isError == false {
self.delegate?.ApplyNowToSendBack() // delegate method
}
}
// Another Viewcontroller
class ShortlistViewController: ParentViewController , QuizPopUpViewControllerDelegate {
}
When i add QuizPopUpViewControllerDelegate to ShortlistViewController, i get the following error:
Type "ShortlistviewController" does not conform to protocol QuizPopUpViewControllerDelegate
The problem is exactly what the error description is suggesting. You need to make your class conforming on QuizPopUpViewControllerDelegate delegate.
To do so, you need to declare the function, what is in QuizPopUpViewControllerDelegate.
// Another Viewcontroller
class ShortlistViewController: ParentViewController , QuizPopUpViewControllerDelegate {
func ApplyNowToSendBack() {
// do something with the callback.
}
}
To avoid asking such questions in the future, i would recommend to read more about the delegate pattern in Swift.
I have a protocol like so :
public protocol SubmitAgeDelegate : class {
func changeSubmitButtonBool()
}
The problem is I want to call it in a generics class.
open class GenericController<UICollectionViewCell,UICollectionReusableView> {
weak var submitAgeDelegate: SubmitAgeDelegate?
Within a UITapGestureRecognizer
func tapGestureDidRecognize(_ gesture: UITapGestureRecognizer) {
if let myAgeDelegate = self.submitAgeDelegate {
print("inside delegate") //Never gets inside
myAgeDelegate.changeSubmitButtonBool()
}
}
Not too sure why it never gets called? Similar ways have worked in regular classes withing IBAction functions.
In my other class :
open class MyCell: ActionCell, SubmitAgeDelegate {
weak var submitAgeDelegate: SubmitAgeDelegate?
public override init(frame: CGRect) {
super.init(frame: frame)
submitAgeDelegate = self
initialize()
}
// Delegate
public func changeSubmitButtonBool(){
print("called ")
}
You are never setting the submitAgeDelegate of the GenericController. Having a member of the same name in your MyCell class doesn't help.
You need to get a reference to your GenericController in order to set its delegate; there's no way around that. (This doesn't have anything to do with generics; it's the same for non-generic classes.) Since it looks like you're using it as a UICollectionViewCell, you might use the reference that you make in tableView:cellForRowAtIndexPath: or similar.
When trying to implement this:
extension UIViewController {
public var presentedViewController: UIViewController? {
return UIViewController()
}
}
I'm receiving the following error:
.../ExampleApp/ExampleAppTests/SpecExtensions.swift:41:59: Getter for 'presentedViewController' with Objective-C selector 'presentedViewController' conflicts with method 'presentedViewController()' with the same Objective-C selector
I'm using the same selector that UIViewController.h defines:
public var presentedViewController: UIViewController? { get }
Is the error misleading or am I just overlooking something? I've tried it with and without override, public, as a method, etc. No luck. However, I am able to override it if it's on a subclass of UIViewController, but not UIViewController itself.
The problem is that you have it in an extension. You can't override methods that were defined on a class from an extension. Basically an extension is not a sub-class, trying to redefine methods that exist on a class by implementing a new version in an extension will fail.
Note that this works:
class ViewController: UIViewController {
override var presentedViewController: UIViewController? {
return UIViewController()
}
}
Here is the description of the delegate property of UIScrollView in apple's document
UIScrollView: UIView, NSCoding {
...
weak public var delegate: UIScrollViewDelegate?
...
}
And the delegate property of UITableView:
UITableView: UIScrollView, NSCoding {
...
weak public var delegate: UITableViewDelegate?
...
}
UITableView inherits from UIScrollView. But the delegate property seems to be independent from that of UIScrollView.There is no override keyword in front of it. And they belong to different types, although UITableViewDelegate inherits from UIScrollViewDelegate. I think it is against the inheriting rule of swift. And i found it did after doing some test in my own class. Here is my test code:
public class SomeClassA {
weak public var delegate: UIScrollViewDelegate?
}
public class SomeClassB: SomeClassA {
//reporting "property 'delegate' with type 'UITableViewDelegate?' cannot
//override a property with type 'UIScrollViewDelegate?'"error
weak public var delegate: UITableViewDelegate?
}
where does the problem lie?