Ramkrishna Sharma
Ramkrishna Sharma

Reputation: 7019

Swift 2.0: Delegate is not working

I have created the class(customAccessaryView) and extends the UIView where I show the Done and Cancel button on UIToolbar which I had loaded from .xib.

Please find the below screenshot for more information.

enter image description here

In my customPickerView class I had declared my protocol methods and delegate and I had bind the doneAction and cancelAction via .xib.

Class : customAccessaryView

@objc protocol customAccessaryDelegate {

  optional func clickONDone()
  optional func clickONCancel()
}

class customAccessaryView : UIView {

 var customView: UIView!
 var delegate:customAccessaryDelegate?    

 override init(frame: CGRect) {
    super.init(frame: frame)

    self.customView = NSBundle.mainBundle().loadNibNamed("CustomAccessary", owner: self, options: nil)[0] as? UIView
    self.addSubview(self.customView)
  }

  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

  }

  @IBAction func doneAction(sender: AnyObject) {

    if let delegate = self.delegate {
        delegate.clickONDone?()
    }

    else {

        print("Not Called")
    }
  }

  @IBAction func cancelAction(sender: AnyObject) {

    if let delegate = self.delegate {
        delegate.clickONCancel?()
    }

    else {            
        print("Not Called")
     }
   }
 }

When I will assign the delegate to another class(createEvent) then the protocol method do not call.

Class : createEvent

class createEvent : UIViewController, customAccessaryDelegate {

  var txtFieldInstance: UITextField!
  var customAV: customAccessaryView!

  override func viewDidLoad() {
        super.viewDidLoad()

    self.customAV = customAccessaryView()
    self.customAV?.delegate = self
  }

  func textFieldShouldBeginEditing(textField: UITextField) -> Bool {

    self.txtFieldInstance = textField
    textField.inputAccessoryView = self.customAV
    return true
  }

  func clickONDone() {

    self.txtFieldInstance.resignFirstResponder()
  }

  func clickONCancel() {
  }
}

As I had used the customAccessaryView for display the inputAccessoryView in keyboard for handling the done and cancel action.

Also I have assigned the delegate and call the desired method but it did not call it.

Any help is much appreciated.

Update

When I forcefully declared the delegate in init method of customAccessaryView class then it is working. Please look into the below code.

  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

     self.delegate = createEvent()
  }

But I did not provide it forcefully.

Upvotes: 3

Views: 678

Answers (1)

deanWombourne
deanWombourne

Reputation: 38475

EDIT: This answer is wrong :) I missed the line in textFieldShouldBeginEditing:.


In your view did load you have the lines

self.customAV = customAccessaryView()
self.customAV?.delegate = self

The first line doesn't do what you're expecting - I assume you want to get the customAccessoryView you created from the storyboard and set it's delegate? What you're actually doing is creating a new CustomAccessoryView and setting it's delegate. You never add it to your view so the instance you're a delegate to isn't the one you can see on your screen.

You need to make your property an outlet

@IBOutlet var customAV: customAccessaryView!

And attach it in the storyboard to the views you've created in there. Then delete the first line from your viewDidLoad so you're always using the one that's in the storyboard.


NB Here's some (probably unwelcome) advice:

Classes in Swift should start with a capital letter i.e. customAccessaryView should really be CustomAccessaryView. Same goes for protocols. Instances of things start with a lowercase letter so you can easily tell them apart when you're reading through the code.

You've made the delegate method optional, but you then use ! when you call it. If you're going to do that, why bother making it optional in the protocol? (or if you want it to be optional, use delegate.clickONDone?() instead.

Upvotes: 1

Related Questions