首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在UIAlertViewDelegate中单击索引按钮未被调用

在UIAlertViewDelegate中单击索引按钮未被调用
EN

Stack Overflow用户
提问于 2014-06-04 12:35:54
回答 2查看 4.9K关注 0票数 2

在Objective之前,我们在UIAlertView中使用了委托:self,这样我们就能够调用方法,比如在index....but上按一下按钮,现在可以在哪里设置该委托?因为我的按钮点击索引方法没有被调用..。

代码语言:javascript
复制
import UIKit

class ViewController:UIViewController,UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate{

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
let cell :UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: nil)
cell.text = "HI \(indexPath.row)"
cell.detailTextLabel.text = "SWIFT"
return cell
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
{
 return 5
}

 func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)
 {


   var alert = UIAlertController(title: "Demo", message: "You have selected row number\(indexPath.row)", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
    alert.addAction(UIAlertAction(title: "KILL", style: UIAlertActionStyle.Destructive, handler: nil))
    alert.addAction(UIAlertAction(title: "PUNE", style: UIAlertActionStyle.Default, handler: nil))
    alert.addAction(UIAlertAction(title: "Mumbai", style: UIAlertActionStyle.Default, handler: nil))
    alert.addAction(UIAlertAction(title: "Kolkata", style: UIAlertActionStyle.Default, handler: nil))
    alert.addAction(UIAlertAction(title: "Chennai", style: UIAlertActionStyle.Default, handler: nil))

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


 }
func demoFunction (myName:NSString)
{
    NSLog("%@", "swapnil")
}

func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int)
{
    NSLog("%@", "YOU PRESSED OK" )
    switch (buttonIndex)
        {
        case 0:NSLog("%@", "YOU PRESSED OK" )
        case 1:NSLog("%@", "YOU PRESSED KILLER" )
        case 2:NSLog("%@", "YOU PRESSED PUNE" )
        case 3:NSLog("%@", "YOU PRESSED MUMBAI" )
        case 4:NSLog("%@", "YOU PRESSED KOLKATA" )
        case 5:NSLog("%@", "YOU PRESSED Chennai" )
        default :NSLog("%@", "You have pressed default")

        }

}

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-04 12:43:41

UIAlertController不实现委托属性。它使用-addAction:的处理程序参数来处理按钮上的点击。下面是如何做到这一点的一个例子:

代码语言:javascript
复制
alert.addAction(UIAlertAction(title: "RandomTitle", style: UIAlertActionStyle.Default, handler:{(alert :UIAlertAction!) -> void in
    //Handle action here. 
    //You can also use the alert title property to differentiate between 
    //UIAlertActions and hand the same closure in to each one. E.i.

    if(action.title == "first option"){
        //handle when the user taps the "first option" button.
    }
}))
票数 1
EN

Stack Overflow用户

发布于 2014-06-13 14:07:16

您不应该使用委托方法,实际上UIAlertController不提供委托。

就你而言,你的警报行动应该是这样的:

代码语言:javascript
复制
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:    {(alert :UIAlertAction!) in
        println("YOU PRESSED OK")

        }))

诸如此类的每一个动作!

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

https://stackoverflow.com/questions/24037612

复制
相关文章

相似问题

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