Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 在UIAlertViewDelegate中的索引处单击的按钮未被调用_Swift - Fatal编程技术网

Swift 在UIAlertViewDelegate中的索引处单击的按钮未被调用

Swift 在UIAlertViewDelegate中的索引处单击的按钮未被调用,swift,Swift,在Objective-C的前面部分,我们在UIAlertView中使用了delegate:self,这样我们就可以调用像在索引处单击按钮这样的方法……但现在在swift中,我在哪里设置该委托?因为没有调用我在索引方法上单击的按钮 import UIKit class ViewController:UIViewController,UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate{ override func viewD

在Objective-C的前面部分,我们在
UIAlertView
中使用了delegate:self,这样我们就可以调用像在索引处单击按钮这样的方法……但现在在swift中,我在哪里设置该委托?因为没有调用我在索引方法上单击的按钮

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")

        }

}

}

UIAlertController
未实现委托属性。它使用处理程序参数
-addAction:
来处理点击按钮的操作。下面是一个如何实现这一点的示例:

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.
    }
}))

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

在您的情况下,您的警报操作应如下所示:

alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:    {(alert :UIAlertAction!) in
        println("YOU PRESSED OK")

        }))
每一个动作都是如此

请您解释一下(alert:UIAlertAction!)我们为什么使用!最后在这里进行手术