UIAlertView clickedButtonAtIndex

时间:2015-07-29 08:55:50

标签: swift ios7 uialertview

我正在实现一个UIAlertView,需要它在iOS7上运行。下面的代码可以工作,但我的问题是关于 alertView(alertView:UIAlertView,clickedButtonAtIndex buttonIndex:Int)功能的行为。

我有一个println("按钮索引点击(buttonIndex)")我用来打印单击的按钮。按钮工作正常,打印0和1。

但是当alertView启动时,我打印出0。这是假设如何工作?或者这里有问题?

  

更新代码

        @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()
    }
}

2 个答案:

答案 0 :(得分:3)

您已在以下行中自行调用:

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

相反,您可以按照here

进行调用

答案 1 :(得分:2)

当alertView启动时,你打印出0。因为在定义alertView self.alertView(purgeAlertController, clickedButtonAtIndex: Int())时强行调用了它的委托方法。

在定义alertView时无需编写此行。评论这一行并尝试一下。剩下的代码是正确的。