1

I'm trying to get a popup alert to add data to an array. Simple, right? I've done it before and it's worked perfectly, but for some reason not today. Here is my code.

@IBAction func addingCat(sender: UIButton) {


    //Creates the alert controller
    var alert = UIAlertController(title: "New Note Category", message: "Please enter the new Note category you wish to create.", preferredStyle: .Alert)

    //Adds the two text fields
    alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
        textField.placeholder = "Title"
        var myColor : UIColor = UIColor( red: 128, green: 0, blue:0, alpha: 1.0 )
        textField.layer.borderColor = myColor.CGColor
    })

    //Saves and prints the values from both text boxes when 'Save' is clicked
    alert.addAction(UIAlertAction(title: "Save", style: .Default, handler: { (action) -> Void in
        let textField = alert.textFields![0] as UITextField
        var titlePrac:String! = "\(textField.text)"

        if titlePrac.isEmpty {
            titlePrac = "New Category"
        }

    }))

    //Presents the alert
    UIAlertController.presentViewController(alert, animated: true, completion: nil) //Extra argument 'animated' in call
    UIApplication.sharedApplication().statusBarStyle = .LightContent

    var newTitle = "        "
    newTitle += "titlePrac"

    sideBarTableViewController.tableData.append(newTitle)
    sideBarTableViewController.tableView.reloadData()
    println("Worked")
    println(sideBarTableViewController.tableData)

}

The error occurs on the first line under my 'Presents the Alert' section. It gives the error:

Extra argument "animated" in call

I have no idea why this is.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Mydogmaxieboy
  • 137
  • 1
  • 1
  • 7

1 Answers1

2

You need to present the alert using current ViewController. You need to change following code:

UIAlertController.presentViewController(alert, animated: true, completion: nil)

to:

self.presentViewController(alert, animated: true, completion: nil)

Also UIAlertController don't have any class method called presentViewController:.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • It now just tells me that 'Missing argument for Parameter #1 in call' – Mydogmaxieboy Jan 03 '15 at 01:13
  • @Mydogmaxieboy: The same code works in my project without any error. Check whether you missed anything there or not – Midhun MP Jan 03 '15 at 01:16
  • The button is embedded in a programmatically created sidebar. So, the code 'self.xxxxx...' doesn't work as 'SideBar does not have a member named presentViewController' To fix this, I changed the code to alert.presentViewController(alert, animated: true, completion: nil) This works until I push the button, and then the app crashes. Really confused @Midhun MP – Mydogmaxieboy Jan 03 '15 at 01:21
  • @Mydogmaxieboy: This will probably help you: http://stackoverflow.com/questions/25505045/display-uialertcontroller-from-uiview-nsobject-class – Midhun MP Jan 03 '15 at 01:30