GuiSoySauce
GuiSoySauce

Reputation: 1783

UIAlertView clickedButtonAtIndex

I am implementing an UIAlertView, need it to run on iOS7. The code bellow works, but my question is about the behaviour of the alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) function.

I have a println("button index clicked (buttonIndex)") I am using to print what button has been clicked. Buttons are working fine, printing 0 and 1.

But also when the alertView initiates I get a 0 printed. Is that how is suppose to work? Or there is something wrong here?

UPDATED CODE

        @IBAction func purgeDatabase() {

    // fetch request
    let fetchRequest = NSFetchRequest(entityName: "ConcreteBagEntity")
    var error : NSError?

    // execute fetch request
    if let fetchResults = self.managedObjectContext!.executeFetchRequest(fetchRequest, error: &error) as? [ConcreteBagEntity] {
        if error != nil{
            return
        }

        // if results are found
        if fetchResults.count != 0 {

            let purgeAlertController = UIAlertView(title: "Before you go ahead", message: "You are about to delete all your previous quotes. Are you sure you want to go ahead?", delegate: self, cancelButtonTitle: "Cancel")
            purgeAlertController.addButtonWithTitle("Ok")
            purgeAlertController.show()

        }// END IF FETCH != 0
        else{
            alertViewLaunch("Looks like there is nothing here to delete")
        }
    }//END FETCH REQUEST
}//END IBACTION

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
    println("button index clicked -> \(buttonIndex)")
    if buttonIndex == 1 {
        purgeAllData()
    }
}

Upvotes: 0

Views: 490

Answers (2)

Munahil
Munahil

Reputation: 2419

You have called it yourself in the following line :

self.alertView(purgeAlertController, clickedButtonAtIndex: Int())

Instead, you can call as mentioned here

Upvotes: 3

VRAwesome
VRAwesome

Reputation: 4803

when the alertView initiates you have get a 0 printed. because you have forcefully called its delegate method at the time of defining alertView self.alertView(purgeAlertController, clickedButtonAtIndex: Int()).

No need to write this line at the time of defining alertView. Comment this line and try it. Remaining code is correct.

Upvotes: 2

Related Questions