3

I wish to create a static method which I can place in a utility class which would launch a UIAlertController. However, I am getting the following error:

"extra argument animated in cell"

static func simpleAlertBox1(msg : String) -> Void{
    let alertController = UIAlertController(title: "Alert!", message: msg, preferredStyle: .actionSheet)
    let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
    alertController.addAction(defaultAction)
    present(alertController, animated: true, completion: nil)// error is being generated here
}

I tried this but it still gave me the same error:

presentViewController(alertController, animated: true, completion: nil)

but if I were to remove the static, then it works fine.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
monekypox121
  • 93
  • 1
  • 8
  • `present` is an instance method of `UIViewController`. You need to call it on an instance of a view controller. Do you have one? – rmaddy Jul 06 '17 at 20:41
  • Compare https://stackoverflow.com/questions/38144019/how-to-create-uialertcontroller-in-global-swift and https://stackoverflow.com/questions/26554894/how-to-present-uialertcontroller-when-not-in-a-view-controller. – Martin R Jul 06 '17 at 20:43
  • why are you using static keyword, that is not the part of swift syntax. – Zohaib Hassan Jul 06 '17 at 20:54
  • yes my static method is in a UIViewController. SO I modified my code to the following: – monekypox121 Jul 06 '17 at 20:56
  • zohaib, because i come from a java background. I am new to swift. I am trying to create a generic UIAlertController. How would you suggest I do that ? – monekypox121 Jul 06 '17 at 20:57

2 Answers2

1

The method present(_:animated:completion:) is an instance method of UIViewController. You need to send that method to a specific instance of UIViewController. By making your function a static function, it is a function of the class, not of an instance of a class.

(It's like sending a message to a car factory saying "set the radio station to 99.5 FM. That message only makes sense when sent to an instance of a car, not to the car factory, or to the entire Toyota Prius class of cars.)

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • if i understand you correctly , you can not have present method in a static method because it belongs to UIViewController, therefore you can not havev a UIAlert in a static method? – monekypox121 Jul 06 '17 at 20:51
  • @monekypox121 You have the `present` method in a `static` method but you can't call it on (the implicit) `self`, you need to call it on a specific view controller instance. – rmaddy Jul 06 '17 at 20:56
  • ok so I modified my code to the following: static func simpleAlertBox1(msg : String) -> UIAlertController{ let alertController = UIAlertController(title: "Alert!", message: msg, preferredStyle: .actionSheet) let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil) alertController.addAction(defaultAction) return alertController } present(simpleAlertBox1(msg: "sdf"), animated: true, completion: nil) but now im getting an error: "expected declaration" – monekypox121 Jul 06 '17 at 20:59
  • @monekypox121 That doesn't change anything. You are still trying to call `present` on `self` in a `static` method. – rmaddy Jul 06 '17 at 21:21
  • Don't post code in comments. It's unreadable. Edit your question to add the new code at the bottom. How are you trying to use this static function? Why do you think it should be a static function rather than an instance method? – Duncan C Jul 06 '17 at 21:41
1

self is an instance of UIViewController , If you want to call this function in a static way just add an other param viewcontroller in which you want to present it. very important, you need to show your alertView after viewDidload.

here an example code:

   class ViewController: UIViewController {

      override func viewDidLoad() {
         super.viewDidLoad()
      }

     override func viewDidAppear(_ animated: Bool) {
         ViewController.simpleAlertBox1(msg: "test", viewController: self)
      }


     static func simpleAlertBox1(msg : String , viewController : UIViewController) -> Void{

       let alertController = UIAlertController(title: "Alert!", message: msg, preferredStyle: .actionSheet)
       let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
       alertController.addAction(defaultAction)
       viewController.present(alertController, animated: true, completion: nil)// error is being generated here

     }
 }
Fares Benhamouda
  • 589
  • 8
  • 21