I want to display my own custom view whenever the cell of the view controller's table view is tapped in my iOS app. And I also have to dismiss the custom view when an user taps a button put inside the custom view.
However, while I was able to display the custom view when the cell is tapped by an user, I cannot dismiss the custom view by tapping the button inside the custom view - it's crashed by the error: unrecognized selector sent to instance...
.
So I wonder what is the correct way to put the button inside the custom view and connect the @IBAction
to it in order to dismiss it. Here's what I did:
Add
@IBAction
connection between the custom view's.xib
and its.swift
file and callself.removeFromSuperview()
from within the method - this makes the app crash with the error message above.Add
@IBAction
connection to my view controller that is called to display the custom view (i.e. one that hasUITableView
) - this cannot be done because when I tried to create the connection, the Xcode doesn't react to the control-drag behaviour.
Also, if I understand it correctly, I cannot add the custom view's xib to the storyboard file because the custom view is not created initially; it's instantiated by tapping the cell on runtime.
So how can I dismiss the custom view by tapping the button? Where should I write the code (i.e. in custom view's .swift
file or the original VC's .swift
file)?
UPDATE
When I added an @IBOutlet
connection from the button on the custom view's xib to the custom view's .swift
file, and tried to println()
from within the view controller such as (println(customView.dismissButton)
), then it also crashed due to the error: this class is not key value coding-compliant for the key dismissButton.
. So I might be better off to just add the gesture controller and make any taps on the window react to dismiss the custom view... It's pretty disgusting.
UPDATE 2
@Caroline's zip file in the comment section is exactly what I wanted to do, but instead of creating and instantiating the custom view all from within code, I want to create the UI on xib, create a @IBAction
connection between the components on the xib and my code, and finally unarchives it from within code to use.