AJS AJS - 1 month ago 18
iOS Question

Ambiguous use of 'dismiss()' on UIButton when converting to swift 3.0

I have programmatically created an UIButton in swift 2.0 named

noBtn
, which signifies the dismissal of an
UIViewController
. When the user clicks the button, the view controller containing the button dismisses. When I converted to swift 3.0 I kept getting this error:


  • Ambiguous use of 'dismiss()'



I tried adding arguments to
dismiss()
but it didn't work. Here is my original code without the arguments.

noBtn.addTarget(self, action: #selector(SetUpNameViewController.dismiss), for: UIControlEvents.touchUpInside)

Answer

Rewrite like this. First, give the same class a dismiss method:

func dismiss() {
    self.dismiss(animated:true)
}

Second, change your selector to #selector(dismiss).

Now your selector will call your dismiss function which will call dismiss(animated:completion:) which is what you are trying to do.