首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIAlertView clickedButtonAtIndex

UIAlertView clickedButtonAtIndex
EN

Stack Overflow用户
提问于 2015-07-29 08:55:50
回答 2查看 480关注 0票数 0

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

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

但是,当alertView启动时,我得到一个0打印。应该是这样运作的吗?还是这里出了什么问题?

更新代码

代码语言:javascript
复制
        @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()
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-29 11:45:09

当alertView启动时,您将得到一个0。因为在定义alertView self.alertView(purgeAlertController, clickedButtonAtIndex: Int())时,您已经强制调用了它的委托方法。

在定义alertView时无需编写这一行。注释这一行并试一试。其余代码是正确的。

票数 2
EN

Stack Overflow用户

发布于 2015-07-29 11:49:10

您自己在下面的一行中这样称呼它:

代码语言:javascript
复制
self.alertView(purgeAlertController, clickedButtonAtIndex: Int())

相反,您可以按前面提到的here调用

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31695735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档