Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Delegate Not Being Called

Tags:

ios

uikit

swift

I have a view with a delegate that I want to have call numpadView(numpadView:) in GameController upon button press, however I can't get it to work. The overload of touchesBegan() works fine, it's really the line with pressDelegate?.numpadView(self) which doesn't call the delegate function in the GameController class. I'm stumped as to what's missing to get it to work?

I cleaned the code to leave only the essential related to the problem for simplicity.

NumpadView.swift

protocol NumpadPressDelegateProtocol {
  func numpadView(numpadView: NumpadView)
}

class NumpadView: UIButton{
  var pressDelegate: NumpadPressDelegateProtocol?

  init(char: Character) {
    let frame = CGRectMake(160, 100, 50, 50)
    super.init(frame:frame)

    self.setTitle("\(char)", forState: UIControlState.Normal)
    self.userInteractionEnabled = true
  }

  override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    pressDelegate?.numpadView(self)
  } 
}

GameController.swift

class GameController: NumpadPressDelegateProtocol {

  func numpadView(numpadView: NumpadView) {
    //do something
  }

}
like image 814
Jeremy Rea Avatar asked Jan 09 '15 03:01

Jeremy Rea


People also ask

How does delegate work Swift?

The Delegate Pattern in Swift The name Delegate comes from the concept of delegating control (giving control to something else). In Swift, a delegate is a controller object with a defined interface that can be used to control or modify the behavior of another object.

What is delegate protocol in Swift?

Protocol: A set of methods that would be implemented by the class which conforms to that protocol. Delegate: The reference to that class which conforms to the protocol and will adhere to implement methods defined in the protocol.

What is the difference delegates and callbacks Swift?

Callbacks are similar in function to the delegate pattern. They do the same thing: letting other objects know when something happened, and passing data around. What differentiates them from the delegate pattern, is that instead of passing a reference to yourself, you are passing a function.

Are there delegates in SwiftUI?

SwiftUI's coordinators are designed to act as delegates for UIKit view controllers. Remember, “delegates” are objects that respond to events that occur elsewhere.


2 Answers

Declaring GameController as NumpadPressDelegateProtocol is not enough for you to get a callback. You also need to set the pressDelegate of the NumpadView instance inside the Gamecontroller. Assuming numpadView is the instance variable be it IBOutlet or not, you have to set the delegate like

Inside GameController init

 numpadView.pressDelegate = self
like image 104
rakeshbs Avatar answered Sep 29 '22 19:09

rakeshbs


Have you set the pressDelegate property to something? Nothing is assigned to it in the code you've shown. Do you assign something to it elsewhere in your code?

like image 40
Dave Wood Avatar answered Sep 29 '22 17:09

Dave Wood